Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support response headers #12

Merged
merged 2 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions features/support/support.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ When(
);

When(
/calling the method ([a-zA-Z]*) and the server responds with/,
/calling the method ([a-zA-Z]*) and the server responds with$/,
async (methodName, response) => {
mock.serverResponseObject = JSON.parse(response);
apiResponse = await api[methodName]();
Expand All @@ -78,7 +78,7 @@ When(
);

When(/extracting the object at index ([0-9]*)/, (index) => {
apiResponse = apiResponse[parseInt(index)];
apiResponse.data = apiResponse.data[parseInt(index)];
});

When(
Expand All @@ -96,6 +96,22 @@ When("selecting the server at index {int}", (index) => {
api = createApi(index);
});

When(
/calling the method ([a-zA-Z]*) and the server responds with headers$/,
async (methodName, headers) => {
mock.serverResponseObject = {};
mock.serverResponseHeaders = JSON.parse(headers);
apiResponse = await api[methodName]();
}
);

Then(
/the response should have a header (.*) with value (.*)/,
(prop, value) => {
assert.equal(apiResponse.headers[prop], value);
}
);

Then(/the request method should be of type (.*)/, (type) => {
assert.equal(mock.requestParams.method, type);
});
Expand All @@ -115,21 +131,21 @@ Then(
);

Then(/the response should be of type (.*)/, (type) => {
assert.equal(apiResponse.constructor.name, type);
assert.equal(apiResponse.data.constructor.name, type);
});

Then(
/the response should have a property ([a-zA-Z]*) with value (.*)/,
(propName, propValue) => {
const value = apiResponse[propName];
const value = apiResponse.data[propName];
const formattedValue =
value instanceof Date ? value.toISOString() : value.toString();
assert.equal(formattedValue, propValue);
}
);

Then(/the response should be an array/, () => {
assert.isArray(apiResponse);
assert.isArray(apiResponse.data);
});

Then(/the requested URL should be (.*)/, (url) => {
Expand All @@ -141,11 +157,11 @@ Then(/the request should have a body with value (.*)/, (body) => {
});

Then(/the response should be equal to "(.*)"/, (value) => {
assert.equal(apiResponse, value);
assert.equal(apiResponse.data, value);
});

Then(/the response should be null/, () => {
assert.isNull(apiResponse);
assert.isNull(apiResponse.data);
});

Then(/it should generate a model object named (\w*)/, (modelName) => {
Expand Down
6 changes: 5 additions & 1 deletion features/support/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ async function generateApi(schema) {

let mock = {
serverResponse: undefined,
serverResponseHeaders: undefined,
};

function createApi(serverIndex = 0) {
Expand All @@ -28,7 +29,10 @@ function createApi(serverIndex = 0) {
const Configuration = require("../api/configuration.js");
const mockTransport = async (params) => {
mock.requestParams = params;
return mock.serverResponseObject;
return {
data: mock.serverResponseObject,
headers: mock.serverResponseHeaders,
};
};

const config = new Configuration(mockTransport);
Expand Down
9 changes: 7 additions & 2 deletions template/api.js.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,14 @@ class Api{{_tag.name}} {

const response = await request(this.config, "{{@root.path}}", "{{@key}}", builder.parameters);
{{#if _response.schema}}
return deserialize(response, "{{typeConvert _response.schema}}");
return {
...response,
data: deserialize(response.data, "{{typeConvert _response.schema}}")
}
{{else}}
return null;
return {
...response
};
{{/if}}
};

Expand Down
18 changes: 14 additions & 4 deletions template/fetch.js.handlebars
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
async function transport(params) {
const response = await fetch(params.url, params);
if (response.status !== 200) {
throw new Error(`${response.status} ${response.statusText}`);
const fetchResponse = await fetch(params.url, params);

if (fetchResponse.ok) {
const json = await fetchResponse.json();
return {
data: json,
statusCode: fetchResponse.status,
headers: fetchResponse.headers,
type: fetchResponse.type,
};
} else {
throw new Error(
`Error ${fetchResponse.status}: ${fetchResponse.statusText}`
);
}
return await response.json();
}

{{{export}}} transport;