|
1 | 1 | import axios from 'axios'
|
2 | 2 | import { v4 as uuidv4 } from 'uuid';
|
3 | 3 | import { Server } from '../sdk/server';
|
| 4 | +import { Stub } from '../sdk/stub'; |
| 5 | +import { Rule } from '../sdk/matching-rule'; |
| 6 | +import { JSONResponse } from '../sdk/response'; |
4 | 7 |
|
5 | 8 | // Your checkout API
|
6 | 9 | const checkoutURL = 'http://localhost:8808/checkout';
|
7 | 10 | const mockServer = new Server('http://localhost:8896');
|
8 | 11 |
|
9 |
| -describe('POST /checkout', () => { |
| 12 | +describe('test /checkout using raw JSON to create stub', () => { |
10 | 13 | // Create random data
|
11 | 14 | const validCardNumber = uuidv4();
|
12 | 15 | const invalidCardNumber = uuidv4();
|
@@ -61,6 +64,46 @@ describe('POST /checkout', () => {
|
61 | 64 | expect(data.response.id).toBe(expectedId)
|
62 | 65 | });
|
63 | 66 |
|
| 67 | + it('card number is not matched, no response from 3rd party', async () => { |
| 68 | + const params = { cardNumber: invalidCardNumber, amount: 30000} |
| 69 | + const { data, status } = await axios.post(checkoutURL, params); |
| 70 | + expect(status).toBe(200) |
| 71 | + expect(data.response).toBeUndefined() |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +describe('test /checkout using SDK to create stub', () => { |
| 76 | + // Create random data |
| 77 | + const validCardNumber = uuidv4(); |
| 78 | + const invalidCardNumber = uuidv4(); |
| 79 | + const expectedId = uuidv4(); |
| 80 | + |
| 81 | + |
| 82 | + beforeAll(async () => { |
| 83 | + try { |
| 84 | + await mockServer.clearStubs(); |
| 85 | + |
| 86 | + // Create a stub which matches method, url and request body (stubs -> request) |
| 87 | + const stub = new Stub('POST', Rule.contains('/pay')) |
| 88 | + .withRequestBodyJSONPath('$.cardNumber', Rule.equalsTo(validCardNumber)) |
| 89 | + .withRequestBodyJSONPath('$.amount', Rule.equalsTo(30000)) |
| 90 | + .willReturn(JSONResponse({id: expectedId})) |
| 91 | + .send(mockServer) |
| 92 | + |
| 93 | + await mockServer.createStub(stub); |
| 94 | + } catch(error) { |
| 95 | + console.log('setup error: ', error); |
| 96 | + } |
| 97 | + }) |
| 98 | + |
| 99 | + it('card number is matched, return predefined body in stub', async () => { |
| 100 | + const params = { cardNumber: validCardNumber, amount: 30000} |
| 101 | + const { data, status } = await axios.post(checkoutURL, params); |
| 102 | + expect(status).toBe(200) |
| 103 | + expect(data.response.id.length).not.toBe(0); |
| 104 | + expect(data.response.id).toBe(expectedId) |
| 105 | + }); |
| 106 | + |
64 | 107 | it('card number is not matched, no response from 3rd party', async () => {
|
65 | 108 | const params = { cardNumber: invalidCardNumber, amount: 30000}
|
66 | 109 | const { data, status } = await axios.post(checkoutURL, params);
|
|
0 commit comments