Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / http / tests / xmlhttprequest / response-encoding.html
blob89de6e3257aec46950e98a697c540b0fc3c14d73
1 <html>
2 <head>
3 <title>Test XmlHttpRequest response encoding handling</title>
4 <meta http-equiv="content-type" content="text/html;charset=koi8-r">
5 <body>
6 <p>Test for <a href="http://bugs.webkit.org/show_bug.cgi?id=5744">bug 5744</a>
7 - correctly determine the encoding of XMLHttpRequest responses.</p>
8 <script>
10 if (window.testRunner) {
11 testRunner.dumpAsText();
12 testRunner.waitUntilDone();
15 var console_messages = document.createElement("ol");
16 document.body.appendChild(console_messages);
18 var asyncStep = 1;
20 function log(message)
22 var item = document.createElement("li");
23 item.appendChild(document.createTextNode(message));
24 console_messages.appendChild(item);
27 function get(url, async)
29 if (window.XMLHttpRequest) {
30 req = new XMLHttpRequest();
31 } else {
32 try {
33 req = new ActiveXObject("Msxml2.XMLHTTP");
34 } catch (ex) {
35 req = new ActiveXObject("Microsoft.XMLHTTP");
39 if (async)
40 req.onreadystatechange = processStateChange;
42 req.open('GET', url, async);
43 req.send(null);
45 if (!async && req.status != 200)
46 throw ("HTTP request failed, status: " + req.status);
48 return req;
51 function getWithOverride(url, contentType, async)
53 if (window.XMLHttpRequest) {
54 req = new XMLHttpRequest();
55 } else {
56 try {
57 req = new ActiveXObject("Msxml2.XMLHTTP");
58 } catch (ex) {
59 req = new ActiveXObject("Microsoft.XMLHTTP");
63 if (async)
64 req.onreadystatechange = processStateChange;
66 req.open('GET', url, async);
67 req.overrideMimeType(contentType);
68 req.send(null);
70 if (!async && req.status != 200)
71 throw ("HTTP request failed, status: " + req.status);
73 return req;
76 function processStateChange(){
77 if (req.readyState == 4){
78 if (req.status == 200){
79 if (asyncStep == 1) {
80 asyncStep = 2;
81 log("Async: Plain text, UTF-8 as default: " + req.responseText);
82 get('resources/reply2.txt', true);
83 } else if (asyncStep == 2) {
84 asyncStep = 3;
85 log("Async: Plain text, windows-1251 specified in HTTP headers: " + req.responseText);
86 getWithOverride('resources/reply3.txt', 'text/plain; charset=windows-1251', true);
87 } else if (asyncStep == 3) {
88 asyncStep = 4;
89 log("Async: Plain text, windows-1251 specified in overrideMimeType: " + req.responseText);
90 getWithOverride('resources/reply4.txt', 'text/plain; charset=windows-1251', true);
91 } else if (asyncStep == 4) {
92 asyncStep = 5;
93 log("Async: Plain text, koi8-r specified in HTTP headers, but overridden by windows-1251 in overrideMimeType: " + req.responseText);
94 get('resources/reply4.xml', true);
95 } else if (asyncStep == 5) {
96 asyncStep = 6;
97 log("Async: XML, UTF-8 as default: " + req.responseXML.getElementsByTagName('node')[0].childNodes[0].nodeValue + ". responseText: " + req.responseText);
98 get('resources/reply.xml', true);
99 } else if (asyncStep == 6) {
100 asyncStep = 7;
101 log("Async: XML, windows-1251 specified in XML declaration: " + req.responseXML.getElementsByTagName('node')[0].childNodes[0].nodeValue + ". responseText: " + req.responseText);
102 get('resources/reply2.xml', true);
103 } else if (asyncStep == 7) {
104 asyncStep = 8;
105 log("Async: XML, koi8-r specified in XML declaration, but overridden by windows-1251 in HTTP headers: " + req.responseXML.getElementsByTagName('node')[0].childNodes[0].nodeValue + ". responseText: " + req.responseText);
106 getWithOverride('resources/reply3.xml', 'text/xml; charset=windows-1251', true);
107 } else if (asyncStep == 8) {
108 asyncStep = 9;
109 log("Async: XML, koi8-r specified in XML declaration, but overridden by windows-1251 in overrideMimeType: " +
110 (req.responseXML ? req.responseXML.getElementsByTagName('node')[0].childNodes[0].nodeValue : "<No responseXML>") + ". responseText: " + req.responseText);
111 get('resources/reply5.txt', true);
112 } else if (asyncStep == 9) {
113 asyncStep = 10;
114 log("Async: XML transferred as text/plain, UTF-8 as default (ignore XML text declaration): " + req.responseText);
115 get('resources/1251.html', true);
116 } else if (asyncStep == 10) {
117 asyncStep = 11;
118 log("Async: HTML, charset determined by a META: " + req.responseText.replace(/\s/g, "").replace(/.*<body>(.*)<\/body>.*/, "$1"));
119 get('resources/utf-8-no-charset.html', true);
120 } else if (asyncStep == 11) {
121 log("Async: HTML, UTF-8 as default: " + req.responseText.replace(/\s/g, "").replace(/.*<body>(.*)<\/body>.*/, "$1"));
122 if (window.testRunner)
123 testRunner.notifyDone();
125 } else {
126 log("Error loading URL: status " + req.status);
131 // sync code
133 // 1
134 try {
135 req = get('resources/reply.txt', false);
136 log("Plain text, UTF-8 as default: " + req.responseText);
137 } catch (ex) {
138 log("Exception: " + ex.description);
141 // 2
142 try {
143 req = get('resources/reply2.txt', false);
144 log("Plain text, windows-1251 specified in HTTP headers: " + req.responseText);
145 } catch (ex) {
146 log("Exception: " + ex.description);
149 // 3
150 try {
151 req = getWithOverride('resources/reply3.txt', 'text/plain; charset=windows-1251', false);
152 log("Plain text, windows-1251 specified in overrideMimeType: " + req.responseText);
153 } catch (ex) {
154 log("Exception: " + ex.description);
157 // 4
158 try {
159 req = getWithOverride('resources/reply4.txt', 'text/plain; charset=windows-1251', false);
160 log("Plain text, koi8-r specified in HTTP headers, but overridden by windows-1251 in overrideMimeType: " + req.responseText);
161 } catch (ex) {
162 log("Exception: " + ex.description);
165 // 5
166 try {
167 req = get('resources/reply4.xml', false);
168 log("XML, UTF-8 as default: " + req.responseXML.getElementsByTagName('node')[0].childNodes[0].nodeValue + ". responseText: " + req.responseText);
169 } catch (ex) {
170 log("Exception: " + ex.description);
173 // 6
174 try {
175 req = get('resources/reply.xml', false);
176 log("XML, windows-1251 specified in XML declaration: " + req.responseXML.getElementsByTagName('node')[0].childNodes[0].nodeValue + ". responseText: " + req.responseText);
177 } catch (ex) {
178 log("Exception: " + ex.description);
181 // 7
182 try {
183 req = get('resources/reply2.xml', false);
184 log("XML, koi8-r specified in XML declaration, but overridden by windows-1251 in HTTP headers: " + req.responseXML.getElementsByTagName('node')[0].childNodes[0].nodeValue + ". responseText: " + req.responseText);
185 } catch (ex) {
186 log("Exception: " + ex.description);
189 // 8
190 try {
191 req = getWithOverride('resources/reply3.xml', 'text/xml; charset=windows-1251', false);
192 log("XML, koi8-r specified in XML declaration, but overridden by windows-1251 in overrideMimeType: " + req.responseXML.getElementsByTagName('node')[0].childNodes[0].nodeValue + ". responseText: " + req.responseText);
193 } catch (ex) {
194 log("Exception: " + ex.description);
197 // 9
198 try {
199 req = get('resources/reply5.txt', false);
200 log("XML transferred as text/plain, UTF-8 as default (ignore XML text declaration): " + req.responseText);
201 } catch (ex) {
202 log("Exception: " + ex.description);
205 // 10
206 try {
207 req = get('resources/1251.html', false);
208 log("HTML, charset determined by a META: " + req.responseText.replace(/\s/g, "").replace(/.*<body>(.*)<\/body>.*/, "$1"));
209 } catch (ex) {
210 log("Exception: " + ex.description);
213 // 11
214 try {
215 req = get('resources/utf-8-no-charset.html', false);
216 log("HTML, UTF-8 as default: " + req.responseText.replace(/\s/g, "").replace(/.*<body>(.*)<\/body>.*/, "$1"));
217 } catch (ex) {
218 log("Exception: " + ex.description);
221 // start async steps
222 get('resources/reply.txt', true);
224 // var console = document.createElement("p");
225 // console.appendChild(console_messages);
226 // document.body.appendChild(console);
228 </script>
229 </body>
230 </html>