Assume: xhr.open("POST", "?q"); xhr.send(payload);

User expects that data shown in NetworkPanel is the same as "payload".
When payload contains non-ASCII chars, this is not true.

That is because postData is decoded with Latin-1 codec.

BUG=136170

Review URL: https://chromiumcodereview.appspot.com/22413002

git-svn-id: svn://svn.chromium.org/blink/trunk@155937 bbb929c8-8fbe-4397-9dbb-9b2b20218538
diff --git a/third_party/WebKit/LayoutTests/http/tests/inspector/network/network-xhr-post-payload-expected.txt b/third_party/WebKit/LayoutTests/http/tests/inspector/network/network-xhr-post-payload-expected.txt
new file mode 100644
index 0000000..b3ee0a7
--- /dev/null
+++ b/third_party/WebKit/LayoutTests/http/tests/inspector/network/network-xhr-post-payload-expected.txt
@@ -0,0 +1,5 @@
+CONSOLE MESSAGE: line 6: XHR loaded: 1
+Tests XHR network resource payload is not corrupted by transcoding.
+
+http://127.0.0.1:8000/inspector/network/resources/resource.php?foo
+
diff --git a/third_party/WebKit/LayoutTests/http/tests/inspector/network/network-xhr-post-payload.html b/third_party/WebKit/LayoutTests/http/tests/inspector/network/network-xhr-post-payload.html
new file mode 100644
index 0000000..3e17454
--- /dev/null
+++ b/third_party/WebKit/LayoutTests/http/tests/inspector/network/network-xhr-post-payload.html
@@ -0,0 +1,33 @@
+<html>
+<head>
+<script src="../inspector-test.js"></script>
+<script src="../network-test.js"></script>
+<script>
+function test()
+{
+    // Since this test could be run together with other inspector backend cache
+    // tests, we need to reset size limits to default ones.
+    InspectorTest.resetInspectorResourcesData(step1);
+    var payload = "\u201AFoo\u201B";
+
+    function step1()
+    {
+        InspectorTest.makeSimpleXHRWithPayload("POST", "resources/resource.php?foo", true, payload, step2);
+    }
+
+    function step2()
+    {
+        var requests = WebInspector.panel("network").requests;
+        var request = requests[requests.length - 1];
+        InspectorTest.addResult(request.url);
+        InspectorTest.assertEquals("foo", request.queryString(), "Unexpected resource query.");
+        InspectorTest.assertEquals(payload, request.requestFormData, "Payload corrupted.");
+        InspectorTest.completeTest();
+    }
+}
+</script>
+</head>
+<body onload="runTest()">
+<p>Tests XHR network resource payload is not corrupted by transcoding.</p>
+</body>
+</html>
diff --git a/third_party/WebKit/Source/core/inspector/InspectorResourceAgent.cpp b/third_party/WebKit/Source/core/inspector/InspectorResourceAgent.cpp
index b3132e5..14c54bb 100644
--- a/third_party/WebKit/Source/core/inspector/InspectorResourceAgent.cpp
+++ b/third_party/WebKit/Source/core/inspector/InspectorResourceAgent.cpp
@@ -220,8 +220,11 @@
         .setUrl(urlWithoutFragment(request.url()).string())
         .setMethod(request.httpMethod())
         .setHeaders(buildObjectForHeaders(request.httpHeaderFields()));
-    if (request.httpBody() && !request.httpBody()->isEmpty())
-        requestObject->setPostData(request.httpBody()->flattenToString());
+    if (request.httpBody() && !request.httpBody()->isEmpty()) {
+        Vector<char> bytes;
+        request.httpBody()->flatten(bytes);
+        requestObject->setPostData(String::fromUTF8WithLatin1Fallback(bytes.data(), bytes.size()));
+    }
     return requestObject;
 }