Skip to content

Commit c16c187

Browse files
committed
Publish
2 parents 8833f39 + 3938a4f commit c16c187

15 files changed

+269
-112
lines changed

.github/workflows/release.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: release
2+
3+
on:
4+
push:
5+
tags:
6+
- v*
7+
8+
jobs:
9+
publish:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v3
13+
- uses: actions/setup-node@v3
14+
with:
15+
node-version: '18.x'
16+
17+
- name: npm install
18+
run: make sdk-install
19+
20+
- run: npm run build
21+
22+
- name: Publish
23+
uses: JS-DevTools/npm-publish@v1
24+
with:
25+
token: ${{ secrets.NPM_TOKEN }}
26+
package: ./sdk

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ This repository provides examples how to use [Rio](https://github.com/hungdv136/
2727

2828
## Structure
2929

30-
- sdk: TypeScript SDK to create and submit stubs to Rio remote server
31-
- example: Examples for using SDK and raw JSON
32-
- server: A sample test target API
33-
- docker: A docker-compose example to deploy Rio
30+
- [sdk](./sdk/README.md): TypeScript SDK to create and submit stubs to Rio remote server
31+
- [example](./example/README.md): Examples for using SDK and raw JSON
32+
- [server](./server/README.md): A sample test target API
33+
- [docker](docker): A docker-compose example to deploy Rio

example/.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"semi": true,
3+
"trailingComma": "none",
4+
"singleQuote": true,
5+
"printWidth": 80
6+
}

example/checkout-sdk.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import axios from 'axios';
2+
import { v4 as uuidv4 } from 'uuid';
3+
import { Server } from '../sdk/server';
4+
import { Stub } from '../sdk/stub';
5+
import { JSONPathRule, Rule } from '../sdk/matching-rule';
6+
import { JSONResponse } from '../sdk/response';
7+
8+
// Your checkout API
9+
const checkoutURL = 'http://localhost:8808/checkout';
10+
const mockServer = new Server('http://localhost:8896');
11+
12+
describe('test /checkout using SDK to create stub', () => {
13+
// Create random data
14+
const validCardNumber = uuidv4();
15+
const invalidCardNumber = uuidv4();
16+
const expectedId = uuidv4();
17+
18+
beforeAll(async () => {
19+
try {
20+
await mockServer.clearStubs();
21+
22+
// Create a stub which matches method, url and request body (stubs -> request)
23+
await new Stub('POST', Rule.contains('/pay'))
24+
.withDescription('stub from sdk')
25+
.withRequestBody(
26+
JSONPathRule('$.cardNumber', Rule.equalsTo(validCardNumber)),
27+
JSONPathRule('$.amount', Rule.equalsTo(30000))
28+
)
29+
.willReturn(JSONResponse({ id: expectedId }))
30+
.send(mockServer);
31+
} catch (error) {
32+
console.log('setup error: ', error);
33+
}
34+
});
35+
36+
it('card number is matched, return predefined body in stub', async () => {
37+
const params = { cardNumber: validCardNumber, amount: 30000 };
38+
const { data, status } = await axios.post(checkoutURL, params);
39+
expect(status).toBe(200);
40+
expect(data.response.id.length).not.toBe(0);
41+
expect(data.response.id).toBe(expectedId);
42+
});
43+
44+
it('card number is not matched, no response from 3rd party', async () => {
45+
const params = { cardNumber: invalidCardNumber, amount: 30000 };
46+
const { data, status } = await axios.post(checkoutURL, params);
47+
expect(status).toBe(200);
48+
expect(data.response).toBeUndefined();
49+
});
50+
});

example/checkout.test.ts

Lines changed: 28 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
1-
import axios from 'axios'
1+
import axios from 'axios';
22
import { v4 as uuidv4 } from 'uuid';
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';
3+
import { Server } from '../sdk/server';
74

85
// Your checkout API
9-
const checkoutURL = 'http://localhost:8808/checkout';
6+
const checkoutURL = 'http://localhost:8808/checkout';
107
const mockServer = new Server('http://localhost:8896');
118

129
describe('test /checkout using raw JSON to create stub', () => {
1310
// Create random data
1411
const validCardNumber = uuidv4();
15-
const invalidCardNumber = uuidv4();
12+
const invalidCardNumber = uuidv4();
1613
const expectedId = uuidv4();
1714

18-
1915
beforeAll(async () => {
2016
try {
2117
await mockServer.clearStubs();
22-
18+
2319
// Create a stub which matches method, url and request body (stubs -> request)
2420
const stub = {
2521
request: {
2622
method: 'POST',
27-
url: [{
23+
url: [
24+
{
2825
name: 'contains',
2926
value: '/pay'
30-
}],
31-
body: [{
32-
content_type: 'application/json',
33-
operator: {
34-
name: 'equal_to',
35-
value: validCardNumber
36-
},
37-
key_path: '$.cardNumber'
38-
}]
27+
}
28+
],
29+
body: [
30+
{
31+
content_type: 'application/json',
32+
operator: {
33+
name: 'equal_to',
34+
value: validCardNumber
35+
},
36+
key_path: '$.cardNumber'
37+
}
38+
]
3939
},
4040
response: {
4141
status_code: 200,
@@ -51,63 +51,23 @@ describe('test /checkout using raw JSON to create stub', () => {
5151
};
5252

5353
await mockServer.createStub(stub);
54-
} catch(error) {
54+
} catch (error) {
5555
console.log('setup error: ', error);
5656
}
57-
})
58-
59-
it('card number is matched, return predefined body in stub', async () => {
60-
const params = { cardNumber: validCardNumber, amount: 30000}
61-
const { data, status } = await axios.post(checkoutURL, params);
62-
expect(status).toBe(200)
63-
expect(data.response.id.length).not.toBe(0);
64-
expect(data.response.id).toBe(expectedId)
6557
});
6658

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-
9959
it('card number is matched, return predefined body in stub', async () => {
100-
const params = { cardNumber: validCardNumber, amount: 30000}
60+
const params = { cardNumber: validCardNumber, amount: 30000 };
10161
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)
62+
expect(status).toBe(200);
63+
expect(data.response.id.length).not.toBe(0);
64+
expect(data.response.id).toBe(expectedId);
10565
});
10666

10767
it('card number is not matched, no response from 3rd party', async () => {
108-
const params = { cardNumber: invalidCardNumber, amount: 30000}
68+
const params = { cardNumber: invalidCardNumber, amount: 30000 };
10969
const { data, status } = await axios.post(checkoutURL, params);
110-
expect(status).toBe(200)
111-
expect(data.response).toBeUndefined()
70+
expect(status).toBe(200);
71+
expect(data.response).toBeUndefined();
11272
});
113-
});
73+
});

example/package-lock.json

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
22
"scripts": {
3-
"test": "jest"
3+
"test": "jest",
4+
"format": "prettier --config .prettierrc '**/*.ts' --write"
45
},
56
"devDependencies": {
67
"@types/jest": "^29.5.2",
78
"jest": "^29.5.0",
9+
"prettier": "^3.0.0",
810
"ts-jest": "^29.1.1",
911
"ts-node": "^10.9.1",
1012
"typescript": "^5.1.6"

example/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"target": "es2016",
44
"module": "commonjs",
55
"esModuleInterop": true,
6-
"forceConsistentCasingInFileNames": true,
6+
"forceConsistentCasingInFileNames": true,
7+
"strictPropertyInitialization": false,
78
"strict": true,
89
"skipLibCheck": true,
910
"strictPropertyInitialization": false

sdk/README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,22 @@ it('checkout API', () => {
1313
const validCardNumber = uuidv4();
1414
const expectedId = uuidv4();
1515

16-
// Submit a stub to mock server
16+
// Submit a stub to simulate 3rd party response
1717
await new Stub()
1818
.withDescription('stub for mocking pay API')
19-
.withRequestBodyJSONPath('$.cardNumber', Rule.equalsTo(validCardNumber))
20-
.withRequestBodyJSONPath('$.amount', Rule.equalsTo(30000))
21-
.willReturn(JSONResponse({id: expectedId}))
19+
.withRequestBody(
20+
JSONPathRule('$.cardNumber', Rule.equalsTo(validCardNumber)),
21+
JSONPathRule('$.amount', Rule.equalsTo(30000))
22+
)
23+
.willReturn(JSONResponse({id: expectedId, amount: 30000}))
2224
.send(mockServer);
2325

24-
// Your test
26+
// Your test case
2527
const params = { cardNumber: validCardNumber, amount: 30000}
2628
const { data, status } = await axios.post(checkoutURL, params);
2729
expect(status).toBe(200)
2830
expect(data.response.id).toBe(expectedId)
2931
});
3032
```
3133

32-
See [example](../example/) for end to end example
34+
See [example](../example/checkout-sdk.test.ts) for end to end example

sdk/constant.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
export const ContentTypeJSON = 'application/json';
22
export const ContentTypeMultiPart = 'multipart/form-data';
33
export const ContentTypeURLEncoded = 'application/x-www-form-urlencoded';
4+
export const ContentTypeHTML = 'text/html';
5+
export const ContentTypeXML = 'text/xml';
46

57
export const HeaderContentType = 'Content-Type';
68
export const HeaderLocation = 'Location';

0 commit comments

Comments
 (0)