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

时间:2019-08-05 07:28:07

标签: soap jestjs supertest

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

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

你应该能够做到这一点:

const xml = "<note><to>Hello</to><from>World</from></note>"
const res = await request(app)
    .post('/')
    .set('Content-Type', 'application/xml')
    .send(xml);