Branch data Line data Source code
1 [ + ]: 1 : import assert from 'node:assert/strict';
2 : 1 : import { XMLHttpRequest } from '../../../lib/whatwg-xhr.js';
3 : 1 :
4 [ + ]: 1 : export default (activeURL) => {
5 : 1 :
6 : 1 : let counter = 0;
7 : 1 :
8 : 1 : status(204, 'UNICORNSWIN', '', '');
9 : 1 : status(401, 'OH HELLO', 'Not today.', '');
10 : 1 : // status(402, 'FIVE BUCKS', '<x>402<\/x>', 'text/xml');
11 : 1 : status(402, 'FREE', 'Nice!', 'text/doesnotmatter');
12 : 1 : status(402, '402 TEH AWESOME', '', '');
13 : 1 : status(502, 'YO', '', '');
14 : 1 : status(502, 'lowercase', 'SWEET POTATO', 'text/plain');
15 : 1 : status(503, 'HOUSTON WE HAVE A', '503', 'text/plain');
16 : 1 : status(699, 'WAY OUTTA RANGE', '699', 'text/plain');
17 : 1 :
18 [ + ]: 1 : function status(code, text, content, type){
19 : 8 : statusRequest('GET', code, text, content, type);
20 : 8 : statusRequest('HEAD', code, text, content, type);
21 : 8 : statusRequest('CHICKEN', code, text, content, type);
22 : 8 : }
23 : 1 :
24 [ + ]: 1 : function statusRequest(method, code, text, content, type){
25 : 24 :
26 : 24 : let xhr = new XMLHttpRequest();
27 : 24 :
28 [ + ]: 24 : xhr.onreadystatechange = (e) => {
29 : 82 :
30 [ + ]: 82 : if(xhr.readyState > 1){
31 : 58 :
32 : 58 : assert.strictEqual(xhr.status, code);
33 : 58 : assert.strictEqual(xhr.statusText, text);
34 : 58 :
35 : 58 : assert.strictEqual(xhr.getResponseHeader('X-Request-Method'), method);
36 : 58 :
37 [ + ]: 58 : if(xhr.readyState === 4){
38 [ + ]: 24 : if(method !== 'HEAD'){
39 [ - ]: 16 : if(type === 'text/xml'){
40 : 0 : assert.strictEqual(xhr.responseXML.documentElement.localName, 'x');
41 : 0 : }
42 : 16 : assert.strictEqual(xhr.responseText, content);
43 : 16 : }
44 : 24 : }
45 [ + ]: 58 : }
46 : 24 : else{
47 : 24 : assert.strictEqual(xhr.status, 0);
48 : 24 : assert.strictEqual(xhr.statusText, '');
49 : 24 : }
50 : 24 : };
51 : 24 :
52 : 24 : let url = `${activeURL}/status.py?code=`;
53 : 24 :
54 : 24 : url += encodeURIComponent(code);
55 : 24 : url += '&text=' + text;
56 : 24 : url += '&content=' + encodeURIComponent(content);
57 : 24 : url += '&type=' + encodeURIComponent(type);
58 : 24 :
59 : 24 : xhr.open(method, url);
60 : 24 : xhr.send();
61 : 24 : }
62 : 1 : }
63 : 1 :
64 : 1 : /*
65 : 1 : * status-async.htm
66 : 1 : *
67 : 1 :
68 : 1 : <!doctype html>
69 : 1 : <html>
70 : 1 : <head>
71 : 1 : <title>XMLHttpRequest: status/statusText - various responses</title>
72 : 1 : <script src="/resources/testharness.js"></script>
73 : 1 : <script src="/resources/testharnessreport.js"></script>
74 : 1 : <link rel="help" href="https://xhr.spec.whatwg.org/#the-status-attribute" data-tested-assertations="following::ol/li[1] following::ol/li[3]" />
75 : 1 : <link rel="help" href="https://xhr.spec.whatwg.org/#the-statustext-attribute" data-tested-assertations="following::ol/li[1] following::ol/li[3]" />
76 : 1 : <link rel="help" href="https://xhr.spec.whatwg.org/#the-getresponseheader()-method" data-tested-assertations="following::ol/li[5]" />
77 : 1 : <link rel="help" href="https://xhr.spec.whatwg.org/#the-responsetext-attribute" data-tested-assertations="following::ol/li[4]" />
78 : 1 : </head>
79 : 1 : <body>
80 : 1 : <div id="log"></div>
81 : 1 : <script>
82 : 1 : var counter=0
83 : 1 : function statusRequest(method, code, text, content, type) {
84 : 1 : counter++
85 : 1 : var test = async_test(document.title +' '+ counter+" (" + method + " " + code + ")")
86 : 1 : test.step(function() {
87 : 1 : var client = new XMLHttpRequest()
88 : 1 : client.onreadystatechange = function(e) {
89 : 1 : test.step(function() {
90 : 1 : if(client.readyState > 1) {
91 : 1 : assert_equals(client.status, code)
92 : 1 : assert_equals(client.statusText, text)
93 : 1 : assert_equals(client.getResponseHeader("X-Request-Method"), method)
94 : 1 : if(client.readyState == 4) {
95 : 1 : if(method != "HEAD") {
96 : 1 : if(type == "text/xml") {
97 : 1 : assert_equals(client.responseXML.documentElement.localName, "x")
98 : 1 : }
99 : 1 : assert_equals(client.responseText, content)
100 : 1 : }
101 : 1 : test.done()
102 : 1 : }
103 : 1 : }else{
104 : 1 : assert_equals(client.status, 0)
105 : 1 : assert_equals(client.statusText, "")
106 : 1 : }
107 : 1 : }, this)
108 : 1 : }
109 : 1 : client.open(method, "resources/status.py?code=" + encodeURIComponent(code) + "&text=" + text + "&content=" + encodeURIComponent(content) + "&type=" + encodeURIComponent(type))
110 : 1 : client.send(null)
111 : 1 : })
112 : 1 : }
113 : 1 : function status(code, text, content, type) {
114 : 1 : statusRequest("GET", code, text, content, type)
115 : 1 : statusRequest("HEAD", code, text, content, type)
116 : 1 : statusRequest("CHICKEN", code, text, content, type)
117 : 1 : }
118 : 1 : status(204, "UNICORNSWIN", "", "")
119 : 1 : status(401, "OH HELLO", "Not today.", "")
120 : 1 : status(402, "FIVE BUCKS", "<x>402<\/x>", "text/xml")
121 : 1 : status(402, "FREE", "Nice!", "text/doesnotmatter")
122 : 1 : status(402, "402 TEH AWESOME", "", "")
123 : 1 : status(502, "YO", "", "")
124 : 1 : status(502, "lowercase", "SWEET POTATO", "text/plain")
125 : 1 : status(503, "HOUSTON WE HAVE A", "503", "text/plain")
126 : 1 : status(699, "WAY OUTTA RANGE", "699", "text/plain")
127 : 1 : </script>
128 : 1 : </body>
129 : 1 : </html>
130 : 1 :
131 : 1 : *
132 : 1 : * status-async.htm
133 : 1 : */
|