3 <title>Test XmlHttpRequest response encoding handling
</title>
4 <meta http-equiv=
"content-type" content=
"text/html;charset=koi8-r">
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>
10 if (window
.testRunner
) {
11 testRunner
.dumpAsText();
12 testRunner
.waitUntilDone();
15 var console_messages
= document
.createElement("ol");
16 document
.body
.appendChild(console_messages
);
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();
33 req
= new ActiveXObject("Msxml2.XMLHTTP");
35 req
= new ActiveXObject("Microsoft.XMLHTTP");
40 req
.onreadystatechange
= processStateChange
;
42 req
.open('GET', url
, async
);
45 if (!async
&& req
.status
!= 200)
46 throw ("HTTP request failed, status: " + req
.status
);
51 function getWithOverride(url
, contentType
, async
)
53 if (window
.XMLHttpRequest
) {
54 req
= new XMLHttpRequest();
57 req
= new ActiveXObject("Msxml2.XMLHTTP");
59 req
= new ActiveXObject("Microsoft.XMLHTTP");
64 req
.onreadystatechange
= processStateChange
;
66 req
.open('GET', url
, async
);
67 req
.overrideMimeType(contentType
);
70 if (!async
&& req
.status
!= 200)
71 throw ("HTTP request failed, status: " + req
.status
);
76 function processStateChange(){
77 if (req
.readyState
== 4){
78 if (req
.status
== 200){
81 log("Async: Plain text, UTF-8 as default: " + req
.responseText
);
82 get('resources/reply2.txt', true);
83 } else if (asyncStep
== 2) {
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) {
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) {
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) {
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) {
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) {
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) {
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) {
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) {
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();
126 log("Error loading URL: status " + req
.status
);
135 req
= get('resources/reply.txt', false);
136 log("Plain text, UTF-8 as default: " + req
.responseText
);
138 log("Exception: " + ex
.description
);
143 req
= get('resources/reply2.txt', false);
144 log("Plain text, windows-1251 specified in HTTP headers: " + req
.responseText
);
146 log("Exception: " + ex
.description
);
151 req
= getWithOverride('resources/reply3.txt', 'text/plain; charset=windows-1251', false);
152 log("Plain text, windows-1251 specified in overrideMimeType: " + req
.responseText
);
154 log("Exception: " + ex
.description
);
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
);
162 log("Exception: " + ex
.description
);
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
);
170 log("Exception: " + ex
.description
);
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
);
178 log("Exception: " + ex
.description
);
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
);
186 log("Exception: " + ex
.description
);
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
);
194 log("Exception: " + ex
.description
);
199 req
= get('resources/reply5.txt', false);
200 log("XML transferred as text/plain, UTF-8 as default (ignore XML text declaration): " + req
.responseText
);
202 log("Exception: " + ex
.description
);
207 req
= get('resources/1251.html', false);
208 log("HTML, charset determined by a META: " + req
.responseText
.replace(/\s/g, "").replace(/.*<body>(.*)<\/body>.*/, "$1"));
210 log("Exception: " + ex
.description
);
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"));
218 log("Exception: " + ex
.description
);
222 get('resources/reply.txt', true);
224 // var console = document.createElement("p");
225 // console.appendChild(console_messages);
226 // document.body.appendChild(console);