|
| 1 | +const { |
| 2 | + resolveRequestBodyForPostmanRequest |
| 3 | + } = require('../../libV2/schemaUtils.js'), |
| 4 | + concreteUtils = require('../../lib/30XUtils/schemaUtils30X'), |
| 5 | + expect = require('chai').expect, |
| 6 | + |
| 7 | + // Example operationItem |
| 8 | + operationItem = { |
| 9 | + put: { |
| 10 | + 'tags': [ |
| 11 | + 'Administration: Users' |
| 12 | + ], |
| 13 | + 'summary': 'Create or Update User', |
| 14 | + 'operationId': 'User', |
| 15 | + 'requestBody': { |
| 16 | + 'content': { |
| 17 | + 'application/json': { |
| 18 | + 'schema': { |
| 19 | + 'type': 'object', |
| 20 | + 'properties': { |
| 21 | + 'foo': { |
| 22 | + 'type': 'string' |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + }, |
| 27 | + 'text/json': { |
| 28 | + 'schema': { |
| 29 | + 'type': 'object', |
| 30 | + 'properties': { |
| 31 | + 'foo': { |
| 32 | + 'type': 'string' |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + }, |
| 37 | + 'application/xml': { |
| 38 | + 'schema': { |
| 39 | + 'type': 'object', |
| 40 | + 'properties': { |
| 41 | + 'foo': { |
| 42 | + 'type': 'string' |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + }, |
| 47 | + 'multipart/form-data': { |
| 48 | + 'schema': { |
| 49 | + 'type': 'object', |
| 50 | + 'properties': { |
| 51 | + 'foo': { |
| 52 | + 'type': 'string' |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + }, |
| 57 | + 'application/x-www-form-urlencoded': { |
| 58 | + 'schema': { |
| 59 | + 'type': 'object', |
| 60 | + 'properties': { |
| 61 | + 'foo': { |
| 62 | + 'type': 'string' |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + }, |
| 68 | + 'description': 'The User request.', |
| 69 | + 'required': true |
| 70 | + }, |
| 71 | + 'responses': { |
| 72 | + '200': { |
| 73 | + 'description': 'OK', |
| 74 | + 'content': { |
| 75 | + 'application/json': { |
| 76 | + 'schema': { |
| 77 | + 'type': 'object', |
| 78 | + 'properties': {} |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | +describe('resolveRequestBodyForPostmanRequest function', function () { |
| 88 | + |
| 89 | + it('should return encoded request body when preferredRequestBodyType is not set', function () { |
| 90 | + const contextTest = { |
| 91 | + concreteUtils, |
| 92 | + schemaCache: {}, |
| 93 | + schemaFakerCache: {}, |
| 94 | + computedOptions: { |
| 95 | + parametersResolution: 'schema' |
| 96 | + } |
| 97 | + }, |
| 98 | + operationItemTest = operationItem.put, |
| 99 | + result = resolveRequestBodyForPostmanRequest(contextTest, operationItemTest); |
| 100 | + |
| 101 | + expect(result.body.mode).to.equal('urlencoded'); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should return raw request body as fallback when preferredRequestBodyType is not a valid option', function () { |
| 105 | + const contextTest = { |
| 106 | + concreteUtils, |
| 107 | + schemaCache: {}, |
| 108 | + schemaFakerCache: {}, |
| 109 | + computedOptions: { |
| 110 | + parametersResolution: 'schema', |
| 111 | + preferredRequestBodyType: 'foo-bar' |
| 112 | + } |
| 113 | + }, |
| 114 | + operationItemTest = operationItem.put, |
| 115 | + result = resolveRequestBodyForPostmanRequest(contextTest, operationItemTest); |
| 116 | + |
| 117 | + expect(result.body.mode).to.equal('raw'); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should return encoded request body when preferredRequestBodyType is x-www-form-urlencoded', function () { |
| 121 | + const contextTest = { |
| 122 | + concreteUtils, |
| 123 | + schemaCache: {}, |
| 124 | + schemaFakerCache: {}, |
| 125 | + computedOptions: { |
| 126 | + parametersResolution: 'schema', |
| 127 | + preferredRequestBodyType: 'x-www-form-urlencoded' |
| 128 | + } |
| 129 | + }, |
| 130 | + operationItemTest = operationItem.put, |
| 131 | + result = resolveRequestBodyForPostmanRequest(contextTest, operationItemTest); |
| 132 | + |
| 133 | + expect(result.body.mode).to.equal('urlencoded'); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should return form data request body when preferredRequestBodyType is form-data', function () { |
| 137 | + const contextTest = { |
| 138 | + concreteUtils, |
| 139 | + schemaCache: {}, |
| 140 | + schemaFakerCache: {}, |
| 141 | + computedOptions: { |
| 142 | + parametersResolution: 'schema', |
| 143 | + preferredRequestBodyType: 'form-data' |
| 144 | + } |
| 145 | + }, |
| 146 | + operationItemTest = operationItem.put, |
| 147 | + result = resolveRequestBodyForPostmanRequest(contextTest, operationItemTest); |
| 148 | + |
| 149 | + expect(result.body.mode).to.equal('formdata'); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should return raw request body when preferredRequestBodyType is raw', function () { |
| 153 | + const contextTest = { |
| 154 | + concreteUtils, |
| 155 | + schemaCache: {}, |
| 156 | + schemaFakerCache: {}, |
| 157 | + computedOptions: { |
| 158 | + parametersResolution: 'schema', |
| 159 | + preferredRequestBodyType: 'raw' |
| 160 | + } |
| 161 | + }, |
| 162 | + operationItemTest = operationItem.put, |
| 163 | + result = resolveRequestBodyForPostmanRequest(contextTest, operationItemTest); |
| 164 | + |
| 165 | + expect(result.body.mode).to.equal('raw'); |
| 166 | + }); |
| 167 | + |
| 168 | + it('should return raw request body when preferredRequestBodyType is first-listed', function () { |
| 169 | + const contextTest = { |
| 170 | + concreteUtils, |
| 171 | + schemaCache: {}, |
| 172 | + schemaFakerCache: {}, |
| 173 | + computedOptions: { |
| 174 | + parametersResolution: 'schema', |
| 175 | + preferredRequestBodyType: 'first-listed' |
| 176 | + } |
| 177 | + }, |
| 178 | + operationItemTest = operationItem.put, |
| 179 | + result = resolveRequestBodyForPostmanRequest(contextTest, operationItemTest); |
| 180 | + |
| 181 | + expect(result.body.mode).to.equal('raw'); |
| 182 | + }); |
| 183 | + |
| 184 | +}); |
0 commit comments