cypress-api-request
Version:
Cypress plugin for RESTful services testing
161 lines (145 loc) • 4.33 kB
JavaScript
import formatHighlight from 'json-format-highlight';
const JSON_COLORS = {
keyColor: "#666",
numberColor: "#333",
stringColor: "#0B7500",
trueColor: "#1177ee",
falseColor: "#1177ee",
nullColor: "#999"
};
const OUTPUT_STYLE = `
<style>
h1 {
color: #333;
background-color: #efefef;
padding: 0.8em;
font-size: 15pt;
font-family: Consolas;
text-align: left;
font-weight: bold;
margin-bottom: 1em;
}
pre {
font-family: 'Courier New';
font-size: 11.5pt;
padding-left: 1em;
line-height: 125%;
}
.request {
color: green;
}
.response {
color: blue;
}
.method {
background: #333;
}
.url {
color: #333;
width: 1000px;
}
.method, .http1xx, .http2xx, .http3xx, .http4xx, .http5xx {
padding: 0.05em 0.3em;
color: #eee;
}
.http1xx, .http2xx, .http3xx, .http4xx, .http5xx {
opacity: 0.75;
}
.http1xx {
background: blue;
}
.http2xx {
background: #0b7500;
}
.http3xx {
background: purple;
}
.http4xx {
background: #bb3300;
}
.http5xx {
background: #bb3300;
}
.checkbox {
width: 1.05em;
height: 1.05em;
vertical-align: middle;
margin-right: 0.5em;
}
.label {
color: #777;
font-family: Consolas;
font-size: 10.5pt;
font-weight: normal;
}
</style>`;
export class DomLogger {
constructor() {
this.isInitialized = false;
Cypress.on("test:before:run", () => {
cy.state("document").body.innerHTML = "";
this.isInitialized = false;
});
}
log(request, response, debugMode) {
let frame = cy.state("document");
let container = frame.body;
if (!this.isInitialized) {
container.innerHTML = OUTPUT_STYLE;
this.isInitialized = true;
}
let fullUrl = new URL(request.url, Cypress.config().baseUrl).href;
let codeFormatted = this.format_http_code(response);
container.innerHTML += `<h1><span class="method">${request.method}</span>
<span class="url">${fullUrl}</span> ${codeFormatted}</h1>`;
let requestObject = debugMode ? this.fullRequest(request, response, fullUrl) : request.body;
let responseObject = debugMode ? this.fullResponse(response) : response.body;
if (requestObject) {
let json = formatHighlight(JSON.stringify(requestObject, null, 4), JSON_COLORS);
json = this.adjust_empty(json);
container.innerHTML += `<pre><b class="request">request</b> = ${json}</pre>`;
}
if (responseObject) {
let json = formatHighlight(JSON.stringify(responseObject, null, 4), JSON_COLORS);
json = this.adjust_empty(json);
container.innerHTML += `<pre><b class="response">response</b> = ${json}</pre>`;
}
Cypress.log({
name: "request",
message: `${request.method} ${response.status} ${fullUrl}`,
consoleProps() {
return {
request: request,
response: response
};
}
});
}
fullRequest(request, response, fullUrl) {
return {
headers: response.requestHeaders,
method: request.method,
url: fullUrl,
body: request.body
};
}
fullResponse(response) {
return {
headers: response.headers,
status: response.status,
statusText: response.statusText,
duration: response.duration,
body: response.body
};
}
adjust_empty(json) {
if (json == "[]" || json == "{}") {
return json;
}
return "\n" + json;
}
format_http_code(response) {
let prefix = parseInt(response.status / 100);
return `<span class="http${prefix}xx">${response.status} ${response.statusText}</span>`;
}
}