@rapidjs.org/testing-http
v0.1.2
Published
rJS Testing – HTTP testing suite.
Downloads
5
Readme
rJS Testing HTTPTest http
rJS Testing HTTP(S) testing suite (HTTPTest
): Test endpoints based on expectation filtered responses.
npm i -D @rapidjs.org/testing-http
npx rjs-testing http <tests-path>
Integrated in
rapidjs-org/testing
.
Configuration
In order to define common request options, they can be defined through the static .configure()
method:
HTTPTest.configure(configuration: RequestOptions & {
pathRoot?: string;
});
Test Anatomy
Expressions
The expressions abstract an initial request (actual) and a successive response (expected).
Actual
.actual(url: string, requestOptions: RequestOptions)
// https://nodejs.org/api/http.html#httprequesturl-options-callback
Expected
interface IResponse { // Request information
status: number; // 200 (default)
headers?: {
[ name: string ]: string;
};
body?: any;
}
.expected(response: IResponse)
Value-based Assertion
new HTTPTest("Get car models")
.actual("/models", {
headers: {
"Content-Type": "application/json"
}
})
.expected({
status: 200,
body: [
{
manufacturer: "Audi",
name: "Q5"
},
{
manufacturer: "Ford",
name: "Bronco"
},
{
manufacturer: "Toyota",
name: "RAV4"
}
]
});
new HTTPTest("Create car model")
.actual("/models", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: {
name: "Q3"
}
})
.expected({
status: 422,
body: "Missing manufacturer name"
});
Comparison Strategy
The comparison strategy does only respected properties stated on the expected response object (down to atomic properties). This is, every leaf-most property on the actual value object – corresponding to the HTTP response – that is not on the expected object is discarded before soft deep equal comparison.
✅ SUCCESS
.actual("/models", {
headers: {
"Content-Type": "application/json"
}
})
/* results in: {
* body: […]
* headers: {
* "Content-Type": "application/json",
* "Content-Length": 225,
* "Server": "rapidJS"
* },
* status: 200,
* […]
* }
*/
.expected({
status: 200,
headers: {
"Content-Length": 225
}
})
❌ FAILURE
.actual("/models", {
headers: {
"Content-Type": "application/json"
}
})
// results in: see above
.expected({
status: 404,
headers: {
"Content-Length": 225
}
})
© Thassilo Martin Schiepanski