Skip to content

Commit 30f17f7

Browse files
committed
test(xhr): add testing for XHR
1 parent 0b8c97a commit 30f17f7

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

tests/js/xhr.simple

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/**
2+
* @file xhr.simple
3+
*
4+
* Simple smoke test which ensures that XMLHttpRequest is initialized, that the
5+
* constructor works, and that it can send requests and receive responses.
6+
*
7+
* @author Caleb Aikens, [email protected]
8+
* @date September 2023
9+
*/
10+
11+
const expectedBody = `<!doctype html>
12+
<html>
13+
<head>
14+
<title>Example Domain</title>
15+
16+
<meta charset="utf-8" />
17+
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
18+
<meta name="viewport" content="width=device-width, initial-scale=1" />
19+
<style type="text/css">
20+
body {
21+
background-color: #f0f0f2;
22+
margin: 0;
23+
padding: 0;
24+
font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
25+
26+
}
27+
div {
28+
width: 600px;
29+
margin: 5em auto;
30+
padding: 2em;
31+
background-color: #fdfdff;
32+
border-radius: 0.5em;
33+
box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
34+
}
35+
a:link, a:visited {
36+
color: #38488f;
37+
text-decoration: none;
38+
}
39+
@media (max-width: 700px) {
40+
div {
41+
margin: 0 auto;
42+
width: auto;
43+
}
44+
}
45+
</style>
46+
</head>
47+
48+
<body>
49+
<div>
50+
<h1>Example Domain</h1>
51+
<p>This domain is for use in illustrative examples in documents. You may use this
52+
domain in literature without prior coordination or asking for permission.</p>
53+
<p><a href="https://www.iana.org/domains/example">More information...</a></p>
54+
</div>
55+
</body>
56+
</html>
57+
`;
58+
59+
new Promise(function (resolve, reject) {
60+
let xhr = new XMLHttpRequest();
61+
xhr.open('GET', 'http://www.example.org/');
62+
63+
xhr.onload = function () {
64+
if (this.status >= 200 && this.status < 300) {
65+
resolve(this.response);
66+
}
67+
else {
68+
reject({
69+
status: this.status,
70+
statusText: this.statusText
71+
});
72+
}
73+
};
74+
75+
xhr.onerror = function () {
76+
reject({
77+
status: this.status,
78+
statusText: this.statusText
79+
});
80+
};
81+
82+
xhr.send();
83+
}).then((value) => {
84+
if (value != expectedBody) {
85+
console.error('expected ', expectedBody, ' but got ', value);
86+
throw new Error('Test failed');
87+
}
88+
console.log('Test passed');
89+
}).catch((error) => {
90+
throw error;
91+
})
92+
93+
94+
new Promise(function (resolve, reject) {
95+
let xhr = new XMLHttpRequest();
96+
xhr.open('POST', 'http://httpbin.org/post');
97+
98+
xhr.onload = function () {
99+
if (this.status >= 200 && this.status < 300) {
100+
resolve(this.response);
101+
}
102+
else {
103+
reject({
104+
status: this.status,
105+
statusText: this.statusText
106+
});
107+
}
108+
};
109+
110+
xhr.onerror = function () {
111+
reject({
112+
status: this.status,
113+
statusText: this.statusText
114+
});
115+
};
116+
117+
xhr.send();
118+
}).then((value) => {
119+
value = JSON.parse(value)
120+
if (!value["headers"]["User-Agent"].startsWith("Python/")) {
121+
console.error("expected Python/* User-Agent, but got ", value.headers["User-Agent"])
122+
}
123+
console.log('Test passed');
124+
}).catch((error) => {
125+
throw error;
126+
})

0 commit comments

Comments
 (0)