|
1 | | -const path = require('path') |
2 | | - |
3 | | -const chai = require('chai') |
4 | | - |
5 | | -const expect = chai.expect |
6 | | - |
7 | | -const { like } = require('pactum-matchers') |
8 | | -const MockServer = require('../../lib/helper/MockServer') |
9 | | -const REST = require('../../lib/helper/REST') |
10 | | - |
11 | | -global.codeceptjs = require('../../lib') |
12 | | - |
13 | | -let I |
14 | | -let restClient |
15 | | -const port = 65000 |
16 | | -const api_url = `http://0.0.0.0:${port}` |
17 | | - |
18 | | -describe('MockServer Helper', function () { |
19 | | - this.timeout(3000) |
20 | | - this.retries(1) |
21 | | - |
22 | | - before(() => { |
23 | | - global.codecept_dir = path.join(__dirname, '/../data') |
24 | | - |
25 | | - I = new MockServer({ port }) |
26 | | - restClient = new REST({ |
27 | | - endpoint: api_url, |
28 | | - defaultHeaders: { |
29 | | - 'X-Test': 'test', |
30 | | - }, |
31 | | - }) |
32 | | - }) |
33 | | - |
34 | | - beforeEach(async () => { |
35 | | - await I.startMockServer() |
36 | | - }) |
37 | | - |
38 | | - afterEach(async () => { |
39 | | - await I.stopMockServer() |
40 | | - }) |
41 | | - |
42 | | - describe('#startMockServer', () => { |
43 | | - it('should start the mock server with custom port', async () => { |
44 | | - global.debugMode = true |
45 | | - await I.startMockServer(6789) |
46 | | - await I.stopMockServer() |
47 | | - global.debugMode = undefined |
48 | | - }) |
49 | | - }) |
50 | | - |
51 | | - describe('#addInteractionToMockServer', () => { |
52 | | - it('should return the correct response', async () => { |
53 | | - await I.addInteractionToMockServer({ |
54 | | - request: { |
55 | | - method: 'GET', |
56 | | - path: '/api/hello', |
57 | | - }, |
58 | | - response: { |
59 | | - status: 200, |
60 | | - body: { |
61 | | - say: 'hello to mock server', |
62 | | - }, |
63 | | - }, |
64 | | - }) |
65 | | - const res = await restClient.sendGetRequest('/api/hello') |
66 | | - expect(res.data).to.eql({ say: 'hello to mock server' }) |
67 | | - }) |
68 | | - |
69 | | - it('should return 404 when not found route', async () => { |
70 | | - const res = await restClient.sendGetRequest('/api/notfound') |
71 | | - expect(res.status).to.eql(404) |
72 | | - }) |
73 | | - |
74 | | - it('should return the strong Match on Query Params', async () => { |
75 | | - await I.addInteractionToMockServer({ |
76 | | - request: { |
77 | | - method: 'GET', |
78 | | - path: '/api/users', |
79 | | - queryParams: { |
80 | | - id: 1, |
81 | | - }, |
82 | | - }, |
83 | | - response: { |
84 | | - status: 200, |
85 | | - body: { |
86 | | - user: 1, |
87 | | - }, |
88 | | - }, |
89 | | - }) |
90 | | - |
91 | | - await I.addInteractionToMockServer({ |
92 | | - request: { |
93 | | - method: 'GET', |
94 | | - path: '/api/users', |
95 | | - queryParams: { |
96 | | - id: 2, |
97 | | - }, |
98 | | - }, |
99 | | - response: { |
100 | | - status: 200, |
101 | | - body: { |
102 | | - user: 2, |
103 | | - }, |
104 | | - }, |
105 | | - }) |
106 | | - let res = await restClient.sendGetRequest('/api/users?id=1') |
107 | | - expect(res.data).to.eql({ user: 1 }) |
108 | | - res = await restClient.sendGetRequest('/api/users?id=2') |
109 | | - expect(res.data).to.eql({ user: 2 }) |
110 | | - }) |
111 | | - |
112 | | - it('should check the stateful behavior', async () => { |
113 | | - await I.addInteractionToMockServer({ |
114 | | - request: { |
115 | | - method: 'GET', |
116 | | - path: '/api/projects/{id}', |
117 | | - pathParams: { |
118 | | - id: like('random-id'), |
119 | | - }, |
120 | | - }, |
121 | | - stores: { |
122 | | - ProjectId: 'req.pathParams.id', |
123 | | - }, |
124 | | - response: { |
125 | | - status: 200, |
126 | | - body: { |
127 | | - id: '$S{ProjectId}', |
128 | | - }, |
129 | | - }, |
130 | | - }) |
131 | | - const res = await restClient.sendGetRequest('/api/projects/10') |
132 | | - expect(res.data).to.eql({ id: '10' }) |
133 | | - }) |
134 | | - }) |
135 | | -}) |
0 commit comments