1 | 'use strict';
|
2 |
|
3 | const assert = require('assert');
|
4 | const harGenerator = require('../harGenerator');
|
5 |
|
6 | const sampleData = {
|
7 | methodUpper: 'GET',
|
8 | baseUrl: 'http://sample.com',
|
9 | requiredUriExample: '/books/1',
|
10 | url: 'http://sample.com/books/{id}',
|
11 | queryParameters: [],
|
12 | allHeaders: []
|
13 | };
|
14 |
|
15 | const sampleHeaders = [
|
16 | { name: 'Accept', exampleValues: { object: 'application/json' } }
|
17 | ];
|
18 |
|
19 | const sampleQueryStringParams = [
|
20 | { name: 'param1', exampleValues: { object: 'value1' } }
|
21 | ];
|
22 |
|
23 | const baseRequest = {
|
24 | httpVersion: 'HTTP/1.1',
|
25 | method: 'GET',
|
26 | url: 'http://sample.com/books/1',
|
27 | queryString: [],
|
28 | headers: [],
|
29 | headersSize: -1,
|
30 | cookies: []
|
31 | }
|
32 |
|
33 | describe('harGenerator tests', () => {
|
34 | it('should generate request for a url', () => {
|
35 | const testData = Object.assign({}, sampleData);
|
36 | const expected = baseRequest;
|
37 |
|
38 | const result = harGenerator.generate(testData);
|
39 |
|
40 | assert.deepEqual(result, expected);
|
41 | });
|
42 |
|
43 | it('should include specified headers in generated request', () => {
|
44 | const testData = Object.assign({}, sampleData, { allHeaders: sampleHeaders });
|
45 | const expected = Object.assign({}, baseRequest, { headers: [{ name: 'Accept', value: 'application/json' }] });
|
46 |
|
47 | const result = harGenerator.generate(testData);
|
48 |
|
49 | assert.deepEqual(result, expected);
|
50 | });
|
51 |
|
52 | it('should include specified query string parameters in generated request', () => {
|
53 | const testData = Object.assign({}, sampleData, { queryParameters: sampleQueryStringParams });
|
54 | const expected = Object.assign({}, baseRequest, { queryString: [{ name: 'param1', value: 'value1' }] });
|
55 |
|
56 | const result = harGenerator.generate(testData);
|
57 |
|
58 | assert.deepEqual(result, expected);
|
59 | });
|
60 |
|
61 | it('should include body payload in generated request, if present', () => {
|
62 | const bodyParameter = {
|
63 | present: true,
|
64 | contentType: 'application/json',
|
65 | exampleValues: {
|
66 | object: { a: 10 },
|
67 | json: JSON.stringify({ a: 10 }, true, 2)
|
68 | }
|
69 | };
|
70 | const testData = Object.assign({}, sampleData, { methodUpper: 'POST', bodyParameter });
|
71 | const expected = Object.assign(
|
72 | {},
|
73 | baseRequest,
|
74 | {
|
75 | method: 'POST',
|
76 | bodySize: -1,
|
77 | postData: { mimeType: 'application/json', text: '{\"a\":10}' }
|
78 | }
|
79 | );
|
80 |
|
81 | const result = harGenerator.generate(testData);
|
82 |
|
83 | assert.deepEqual(result, expected);
|
84 | });
|
85 |
|
86 | it('should use postData.params in case of multipart/form-data payload', () => {
|
87 | const bodyParameter = {
|
88 | present: true,
|
89 | contentType: 'multipart/form-data',
|
90 | exampleValues: {
|
91 | object: { a: 10 },
|
92 | json: JSON.stringify({ a: 10 }, true, 2)
|
93 | }
|
94 | };
|
95 | const testData = Object.assign({}, sampleData, { methodUpper: 'POST', bodyParameter });
|
96 | const expected = Object.assign(
|
97 | {},
|
98 | baseRequest,
|
99 | {
|
100 | method: 'POST',
|
101 | bodySize: -1,
|
102 | postData: { mimeType: 'multipart/form-data', params: [{ name: "a", value: 10 }] }
|
103 | }
|
104 | );
|
105 |
|
106 | const result = harGenerator.generate(testData);
|
107 |
|
108 | assert.deepEqual(result, expected);
|
109 | });
|
110 |
|
111 | it('should use operation url if uri example is not present', () => {
|
112 | const testData = Object.assign({}, sampleData, { requiredUriExample: null });
|
113 | const expected = Object.assign({}, baseRequest, { url: 'http://sample.com/books/{id}' });
|
114 |
|
115 | const result = harGenerator.generate(testData);
|
116 |
|
117 | assert.deepEqual(result, expected);
|
118 | });
|
119 | });
|