Skip to content

Commit

Permalink
Add x-www-form-urlencoded media type examples support (#67)
Browse files Browse the repository at this point in the history
* Add x-www-form-urlencoded media type

* ditch querystring dependency

* escape keys and values

* add test

* fix: replace all '%20' occurrences

* fix: use replace function with wildcard instead of replaceAll

* improve syntax

Co-authored-by: Diogo Silva <[email protected]>
  • Loading branch information
d-silva and dsilva-alviere authored Aug 13, 2021
1 parent b999cd0 commit 52b9e66
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 13 deletions.
56 changes: 43 additions & 13 deletions openapi-to-har.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,51 @@ const getPayload = function (openApi, path, method) {

if (
openApi.paths[path][method].requestBody &&
openApi.paths[path][method].requestBody.content &&
openApi.paths[path][method].requestBody.content['application/json'] &&
openApi.paths[path][method].requestBody.content['application/json'].schema
openApi.paths[path][method].requestBody.content
) {
const sample = OpenAPISampler.sample(
openApi.paths[path][method].requestBody.content['application/json']
.schema,
{ skipReadOnly: true },
openApi
);
return {
mimeType: 'application/json',
text: JSON.stringify(sample),
};
if (openApi.paths[path][method].requestBody.content['application/json'] &&
openApi.paths[path][method].requestBody.content['application/json'].schema
) {
const sample = OpenAPISampler.sample(
openApi.paths[path][method].requestBody.content['application/json']
.schema,
{ skipReadOnly: true },
openApi
);
return {
mimeType: 'application/json',
text: JSON.stringify(sample)
};
}

if (openApi.paths[path][method].requestBody.content['application/x-www-form-urlencoded'] &&
openApi.paths[path][method].requestBody.content['application/x-www-form-urlencoded'].schema
) {
const sample = OpenAPISampler.sample(openApi.paths[path][method].requestBody.content['application/x-www-form-urlencoded']
.schema,
{skipReadOnly: true},
openApi
);

if (sample === undefined) return null;

const params = [];
Object.keys(sample).map(
key => params.push({
'name': encodeURIComponent(key).replace(/\%20/g, '+'),
'value': encodeURIComponent(sample[key]).replace(/\%20/g, '+')
})
}
);

return {
mimeType: 'application/x-www-form-urlencoded',
params: params,
text: Object.keys(params).map(key => key + '=' + sample[key]).join('&')
};
}
}

return null;
};

Expand Down
43 changes: 43 additions & 0 deletions test/form_urlencoded_example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"openapi": "3.0.0",
"info": {
"version": "1.0.0",
"title": "Sample API",
"description": "A sample API to demonstrate x-www-form-encoded request body example"
},
"servers": [
{
"url": "http://auth.io/api"
}
],
"paths": {
"/auth/token": {
"post": {
"requestBody": {
"required": true,
"content": {
"application/x-www-form-urlencoded": {
"schema": {
"type": "object",
"required": [
"id",
"secret"
],
"properties": {
"id": {
"type": "string",
"example": "id example value"
},
"secret": {
"type": "string",
"example": "secret example value"
}
}
}
}
}
}
}
}
}
}
15 changes: 15 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const PetStoreOpenAPI = require('./petstore_swagger.json');
const PetStoreOpenAPI3 = require('./petstore_oas.json');
const ParameterSchemaReferenceAPI = require('./parameter_schema_reference');
const ParameterExampleReferenceAPI = require('./parameter_example_swagger.json');
const FormUrlencodedExampleAPI = require('./form_urlencoded_example.json');

test('Getting snippets should not result in error or undefined', function (t) {
t.plan(1);
Expand Down Expand Up @@ -196,3 +197,17 @@ test('Testing the case when an example is provided, use the provided example val
t.false(/SOME_INTEGER_VALUE/.test(snippet));
t.end();
});

test('Testing the application/x-www-form-urlencoded example case', function (t) {
t.plan(2);
const result = OpenAPISnippets.getEndpointSnippets(
FormUrlencodedExampleAPI,
'/auth/token',
'post',
['shell_curl']
);
const snippet = result.snippets[0].content;
t.match(snippet, /.*--data 'id=id\+example\+value'.*/);
t.match(snippet, /.*--data 'secret=secret\+example\+value'.*/);
t.end();
});

0 comments on commit 52b9e66

Please sign in to comment.