如何通过Jest和Supertest发送SOAP请求?

问题描述 投票:0回答:1

我需要发送一个SOAP请求,它是一个XML文件。

如何发送作为XML文件的内容作为后期处理请求?

soap jestjs supertest
1个回答
0
投票

这里是一个例子:

app.js

const express = require('express');
const app = express();
const xmlparser = require('express-xml-bodyparser');

app.use(xmlparser());
app.post('/', (req, res) => {
  res.json(req.body);
});

module.exports = app;

app.test.js

const app = require('./app');
const request = require('supertest');

describe('57353993', () => {
  // read from XML file
  var itemsXML = '<list><item>item1</item><item>item2</item><item>item3</item></list>';
  var itemList = {
    list: {
      item: ['item1', 'item2', 'item3'],
    },
  };
  it('should pass', async () => {
    const res = await request(app)
      .post('/')
      .set('Content-Type', 'application/xml')
      .send(itemsXML);
    expect(res.status).toBe(200);
    expect(res.body).toEqual(itemList);
  });
});

测试结果:

 PASS  src/stackoverflow/57353993/app.test.js (9.465s)
  57353993
    ✓ should pass (56ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        10.598s, estimated 11s
© www.soinside.com 2019 - 2024. All rights reserved.