-
Notifications
You must be signed in to change notification settings - Fork 150
/
http-adapters-test.ts
99 lines (87 loc) · 2.38 KB
/
http-adapters-test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import nock from 'nock';
import { expect } from 'chai';
import * as requestPromise from '../example/request-promise';
import * as nodeFetch from '../example/node-fetch';
type AdapterConfig = {
name: string;
callBackend: typeof requestPromise.callBackend;
}[];
const adapterConfig: AdapterConfig = [
{
name: 'request-promise',
callBackend: requestPromise.callBackend,
},
{
name: 'node-fetch',
callBackend: nodeFetch.callBackend,
},
];
adapterConfig.forEach(({ name, callBackend }) => {
describe(name, () => {
beforeEach(() => {
nock.disableNetConnect();
nock.enableNetConnect('127.0.0.1');
});
afterEach(() => {
nock.cleanAll();
nock.enableNetConnect();
});
it('should make json http calls', async () => {
const nockScope = nock('http://mock-host')
.post('/mock-uri', {
mockBodyKey: 'mock body value',
})
.query({
'query-params': 'a,b',
})
.matchHeader('mock-header', 'mock header value')
.matchHeader('content-type', 'application/json')
.reply(200, 'mock result');
const result = await callBackend({
requestOptions: {
method: 'post',
baseUrl: 'http://mock-host',
path: '/mock-uri',
headers: {
'mock-header': 'mock header value',
},
query: {
'query-params': ['a', 'b'],
},
body: {
mockBodyKey: 'mock body value',
},
bodyType: 'json',
},
context: {},
});
expect(result).to.equal('mock result');
nockScope.done();
});
it('should post formData', async () => {
const nockScope = nock('http://mock-host')
.post('/mock-uri', body => {
expect(body).to.deep.equal({
mockBodyKey: 'mock body value',
});
return true;
})
.matchHeader('content-type', /application\/x-www-form-urlencoded/)
.reply(200, 'mock result');
const result = await callBackend({
requestOptions: {
method: 'post',
baseUrl: 'http://mock-host',
path: '/mock-uri',
body: {
mockBodyKey: 'mock body value',
},
bodyType: 'formData',
},
context: {},
});
expect(result).to.equal('mock result');
nockScope.done();
});
});
});