Skip to content

Commit c5ffeb6

Browse files
author
Stewart Miles
committed
Fixed measurement in Unity 5.x.
Fixed NullReferenceException in PortableWebRequest when a post request is performed with no headers. Change-Id: Ibc466043cb281de3835e1fa860f5ac1e1db7a708
1 parent 1ae4b0e commit c5ffeb6

File tree

2 files changed

+108
-17
lines changed

2 files changed

+108
-17
lines changed

source/VersionHandlerImpl/src/PortableWebRequest.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,12 @@ private static object StartRequest(HttpMethod method, string url,
441441
object unityRequest = null;
442442
if (FindAndCacheRequestType()) {
443443
if (requestType.Name == "WWW") {
444-
var uploadHeaders = new Dictionary<string, string>(headers);
444+
var uploadHeaders = new Dictionary<string, string>();
445+
if (headers != null) {
446+
foreach (var kv in headers) {
447+
uploadHeaders[kv.Key] = kv.Value;
448+
}
449+
}
445450
// Need to manually add the Content-Type header when sending a form as the
446451
// constructor that takes a WWWForm that doesn't allow the specification of
447452
// headers.
@@ -485,7 +490,9 @@ public IPortableWebRequestStatus Post(
485490
string url, IDictionary<string, string> headers,
486491
IEnumerable<KeyValuePair<string, string>> formFields) {
487492
var form = new WWWForm();
488-
foreach (var formField in formFields) form.AddField(formField.Key, formField.Value);
493+
if (formFields != null) {
494+
foreach (var formField in formFields) form.AddField(formField.Key, formField.Value);
495+
}
489496

490497
try {
491498
return StartRequestOnMainThread(HttpMethod.Post, url, headers, form);

source/VersionHandlerImpl/test/webrequest/Assets/PlayServicesResolver/Editor/TestPortableWebRequest.cs_DISABLED

Lines changed: 99 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@ public class TestPortableWebRequest {
2525

2626
// Whether the get operation succeeded.
2727
private static bool getSucceeded = false;
28+
// Whether the get operation with no query succeeded.
29+
private static bool getNoQuerySucceeded = false;
30+
// Whether the get operation with no headers succeeded.
31+
private static bool getNoHeadersSucceeded = false;
2832
// Whether the post operation succeeded.
2933
private static bool postSucceeded = false;
34+
// Whether the post operation with no headers succeeded.
35+
private static bool postNoHeadersSucceeded = false;
3036

3137
/// <summary>
3238
/// Register a method to call when the Version Handler has enabled all plugins in the project.
@@ -92,18 +98,68 @@ public class TestPortableWebRequest {
9298
"echo-bish=Bosh\n" +
9399
"echo-foo=Bar");
94100
var result = System.Text.Encoding.Default.GetString(getStatus.Result);
95-
var expected = ("{\"data\": \"Hello from a test server\", " +
96-
"\"headers\": {\"Echo-Bish\": \"Bosh\", " +
97-
"\"Echo-Foo\": \"Bar\"}, " +
98-
"\"path\": \"/get?foo1=bar1&foo2=bar2\", " +
99-
"\"query\": {\"foo1\": [\"bar1\"], \"foo2\": [\"bar2\"]}}");
101+
var expected =
102+
"{\"data\": \"Hello from a test server\", " +
103+
"\"headers\": {\"Echo-Bish\": \"Bosh\", \"Echo-Foo\": \"Bar\"}, " +
104+
"\"path\": \"/get?foo1=bar1&foo2=bar2\", " +
105+
"\"query\": {\"foo1\": [\"bar1\"], \"foo2\": [\"bar2\"]}}";
100106
getSucceeded &= CheckEqual(result, expected);
101107
UnityEngine.Debug.Log(String.Format("Get complete succeeded={0}\n{1}",
102108
getSucceeded, result));
103109
}
104110
return complete;
105111
}, synchronous: true);
106112

113+
UnityEngine.Debug.Log("Running get with no query test...");
114+
var getStatusNoQuery = webRequest.Get("http://localhost:8000/get_with_no_query",
115+
new Dictionary<string, string> {
116+
{ "Echo-Foo", "Bar" },
117+
{ "Echo-Bish", "Bosh" }
118+
});
119+
RunOnMainThread.PollOnUpdateUntilComplete(() => {
120+
var complete = getStatusNoQuery.Complete;
121+
if (complete) {
122+
getNoQuerySucceeded = CheckEqual(getStatusNoQuery.Status, HttpStatusCode.OK);
123+
getNoQuerySucceeded &= CheckEqual(
124+
DictionaryToString("echo-", true, getStatusNoQuery.Headers),
125+
"echo-bish=Bosh\n" +
126+
"echo-foo=Bar");
127+
var result = System.Text.Encoding.Default.GetString(getStatusNoQuery.Result);
128+
var expected =
129+
"{\"data\": \"Hello from a test server\", " +
130+
"\"headers\": {\"Echo-Bish\": \"Bosh\", \"Echo-Foo\": \"Bar\"}, " +
131+
"\"path\": \"/get_with_no_query\", " +
132+
"\"query\": {}}";
133+
getNoQuerySucceeded &= CheckEqual(result, expected);
134+
UnityEngine.Debug.Log(String.Format("Get with no query succeeded={0}\n{1}",
135+
getNoQuerySucceeded, result));
136+
}
137+
return complete;
138+
}, synchronous: true);
139+
140+
UnityEngine.Debug.Log("Running get with no headers test...");
141+
var getStatusNoHeaders = webRequest.Get("http://localhost:8000/get?foo1=bar1&foo2=bar2",
142+
null);
143+
RunOnMainThread.PollOnUpdateUntilComplete(() => {
144+
var complete = getStatusNoHeaders.Complete;
145+
if (complete) {
146+
getNoHeadersSucceeded = CheckEqual(getStatusNoHeaders.Status,
147+
HttpStatusCode.OK);
148+
getNoHeadersSucceeded &= CheckEqual(DictionaryToString("echo-", true,
149+
getStatusNoHeaders.Headers), "");
150+
var result = System.Text.Encoding.Default.GetString(getStatusNoHeaders.Result);
151+
var expected =
152+
"{\"data\": \"Hello from a test server\", " +
153+
"\"headers\": {}, " +
154+
"\"path\": \"/get?foo1=bar1&foo2=bar2\", " +
155+
"\"query\": {\"foo1\": [\"bar1\"], \"foo2\": [\"bar2\"]}}";
156+
getNoHeadersSucceeded &= CheckEqual(result, expected);
157+
UnityEngine.Debug.Log(String.Format("Get with no headers succeeded={0}\n{1}",
158+
getNoHeadersSucceeded, result));
159+
}
160+
return complete;
161+
}, synchronous: true);
162+
107163
UnityEngine.Debug.Log("Running post test...");
108164
var postStatus = webRequest.Post("http://localhost:8000/post",
109165
new Dictionary<string, string> {
@@ -117,27 +173,55 @@ public class TestPortableWebRequest {
117173
RunOnMainThread.PollOnUpdateUntilComplete(() => {
118174
var complete = postStatus.Complete;
119175
if (complete) {
120-
postSucceeded = CheckEqual(getStatus.Status, HttpStatusCode.OK);
121-
postSucceeded &= CheckEqual(DictionaryToString("echo-", true,
122-
getStatus.Headers),
123-
"echo-bish=Bosh\n" +
124-
"echo-foo=Bar");
176+
postSucceeded = CheckEqual(postStatus.Status, HttpStatusCode.OK);
177+
postSucceeded &= CheckEqual(
178+
DictionaryToString("echo-", true, postStatus.Headers),
179+
"echo-bish=Bosh\n" +
180+
"echo-foo=Bar");
125181
var result = System.Text.Encoding.Default.GetString(postStatus.Result);
126182
var expected =
127183
"{\"data\": \"Hello from a test server\", " +
128-
"\"form\": {\"foo1\": [\"bar1\"], \"foo2\": " +
129-
"[\"bar2\"]}, " +
130-
"\"headers\": {\"Echo-Bish\": \"Bosh\", " +
131-
"\"Echo-Foo\": \"Bar\"}, \"path\": \"/post\", \"query\": {}}";
184+
"\"form\": {\"foo1\": [\"bar1\"], \"foo2\": [\"bar2\"]}, " +
185+
"\"headers\": {\"Echo-Bish\": \"Bosh\", \"Echo-Foo\": \"Bar\"}, " +
186+
"\"path\": \"/post\", \"query\": {}}";
132187
postSucceeded &= CheckEqual(result, expected);
133188
UnityEngine.Debug.Log(String.Format("Post complete succeeded={0}\n{1}",
134189
postSucceeded, result));
135190
}
136191
return complete;
137192
}, synchronous: true);
138193

194+
UnityEngine.Debug.Log("Running post test with no headers...");
195+
var postNoHeadersStatus = webRequest.Post(
196+
"http://localhost:8000/post/with/no/headers",
197+
null,
198+
new[] {
199+
new KeyValuePair<string, string>("foo1", "bar1"),
200+
new KeyValuePair<string, string>("foo2", "bar2")
201+
});
202+
RunOnMainThread.PollOnUpdateUntilComplete(() => {
203+
var complete = postNoHeadersStatus.Complete;
204+
if (complete) {
205+
postNoHeadersSucceeded = CheckEqual(postNoHeadersStatus.Status,
206+
HttpStatusCode.OK);
207+
postNoHeadersSucceeded &= CheckEqual(DictionaryToString(
208+
"echo-", true, postNoHeadersStatus.Headers), "");
209+
var result = System.Text.Encoding.Default.GetString(postNoHeadersStatus.Result);
210+
var expected =
211+
"{\"data\": \"Hello from a test server\", " +
212+
"\"form\": {\"foo1\": [\"bar1\"], \"foo2\": [\"bar2\"]}, " +
213+
"\"headers\": {}, " +
214+
"\"path\": \"/post/with/no/headers\", \"query\": {}}";
215+
postNoHeadersSucceeded &= CheckEqual(result, expected);
216+
UnityEngine.Debug.Log(String.Format("Post with no headers succeeded={0}\n{1}",
217+
postNoHeadersSucceeded, result));
218+
}
219+
return complete;
220+
}, synchronous: true);
221+
139222
// Exit when the tests are complete.
140-
if (!(getSucceeded && postSucceeded)) {
223+
if (!(getSucceeded && getNoQuerySucceeded && getNoHeadersSucceeded && postSucceeded
224+
&& postNoHeadersSucceeded)) {
141225
UnityEngine.Debug.Log("Test failed");
142226
UnityEditor.EditorApplication.Exit(1);
143227
}

0 commit comments

Comments
 (0)