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

fix(request-duration): Set response duration in failure cases #10064

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion src/core/plugins/spec/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,9 @@ export const executeRequest = (req) =>
err.message = "**Failed to fetch.** \n**Possible Reasons:** \n - CORS \n - Network Failure \n - URL scheme must be \"http\" or \"https\" for CORS request."
}
specActions.setResponse(req.pathName, req.method, {
error: true, err
error: true,
duration: Date.now() - startTime,
err,
})
}
)
Expand Down
49 changes: 49 additions & 0 deletions test/unit/core/plugins/spec/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,55 @@ describe("spec plugin - actions", function(){
expect(system.specActions.setMutatedRequest.mock.calls.length).toEqual(1)
expect(system.specActions.setRequest.mock.calls.length).toEqual(1)
})

it("should include duration in the response on error", async () => {
let configs = {
requestInterceptor: jest.fn(),
responseInterceptor: jest.fn()
}

const fakeDelay = 100
const system = {
fn: {
buildRequest: jest.fn(),
execute: jest.fn().mockImplementation(() => new Promise((_, reject) => {
setTimeout(() => {
reject(new Error("Error message"))
}, fakeDelay)
}))
},
specActions: {
executeRequest: jest.fn(),
setMutatedRequest: jest.fn(),
setRequest: jest.fn(),
setResponse: jest.fn()
},
specSelectors: {
spec: () => fromJS({}),
parameterValues: () => fromJS({}),
contentTypeValues: () => fromJS({}),
url: () => fromJS({}),
isOAS3: () => false
},
getConfigs: () => configs
}

const executeFn = executeRequest({
pathName: "/error",
method: "GET",
operation: fromJS({operationId: "getError"})
})

await executeFn(system)

expect(system.fn.execute.mock.calls.length).toEqual(1)
expect(system.specActions.setResponse).toHaveBeenCalled()

// Check if duration was set and is at least 100ms
const callArgs = system.specActions.setResponse.mock.calls[0][2]
expect(callArgs).toHaveProperty("duration")
expect(callArgs.duration).toBeGreaterThanOrEqual(fakeDelay)
})
})

xit("should call specActions.setResponse, when fn.execute resolves", function(){
Expand Down