OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.net; | 5 package org.chromium.net; |
6 | 6 |
7 import android.os.ConditionVariable; | 7 import android.os.ConditionVariable; |
8 import android.test.suitebuilder.annotation.SmallTest; | 8 import android.test.suitebuilder.annotation.SmallTest; |
9 | 9 |
10 import org.chromium.base.test.util.DisabledTest; | 10 import org.chromium.base.test.util.DisabledTest; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 for (int i = 0; i < headers.length; i += 2) { | 77 for (int i = 0; i < headers.length; i += 2) { |
78 headersList.add(new AbstractMap.SimpleImmutableEntry<String, String>
( | 78 headersList.add(new AbstractMap.SimpleImmutableEntry<String, String>
( |
79 headers[i], headers[i + 1])); | 79 headers[i], headers[i + 1])); |
80 } | 80 } |
81 UrlResponseInfo urlResponseInfo = new UrlResponseInfo( | 81 UrlResponseInfo urlResponseInfo = new UrlResponseInfo( |
82 Arrays.asList(urls), statusCode, message, headersList, false, "h
2", null); | 82 Arrays.asList(urls), statusCode, message, headersList, false, "h
2", null); |
83 urlResponseInfo.setReceivedBytesCount(receivedBytes); | 83 urlResponseInfo.setReceivedBytesCount(receivedBytes); |
84 return urlResponseInfo; | 84 return urlResponseInfo; |
85 } | 85 } |
86 | 86 |
| 87 private void runSimpleGetWithExpectedReceivedBytesCount(int expectedReceived
Bytes) |
| 88 throws Exception { |
| 89 String url = Http2TestServer.getEchoMethodUrl(); |
| 90 TestBidirectionalStreamCallback callback = new TestBidirectionalStreamCa
llback(); |
| 91 // Create stream. |
| 92 BidirectionalStream stream = new BidirectionalStream |
| 93 .Builder(url, callback, callback.ge
tExecutor(), |
| 94 mTestFramework.mCronetEngin
e) |
| 95 .setHttpMethod("GET") |
| 96 .build(); |
| 97 stream.start(); |
| 98 callback.blockForDone(); |
| 99 assertTrue(stream.isDone()); |
| 100 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
| 101 // Default method is 'GET'. |
| 102 assertEquals("GET", callback.mResponseAsString); |
| 103 UrlResponseInfo urlResponseInfo = createUrlResponseInfo( |
| 104 new String[] {url}, "", 200, expectedReceivedBytes, ":status", "
200"); |
| 105 assertResponseEquals(urlResponseInfo, callback.mResponseInfo); |
| 106 checkResponseInfo(callback.mResponseInfo, Http2TestServer.getEchoMethodU
rl(), 200, ""); |
| 107 } |
| 108 |
87 @SmallTest | 109 @SmallTest |
88 @Feature({"Cronet"}) | 110 @Feature({"Cronet"}) |
89 public void testBuilderChecks() throws Exception { | 111 public void testBuilderChecks() throws Exception { |
90 TestBidirectionalStreamCallback callback = new TestBidirectionalStreamCa
llback(); | 112 TestBidirectionalStreamCallback callback = new TestBidirectionalStreamCa
llback(); |
91 try { | 113 try { |
92 new BidirectionalStream.Builder( | 114 new BidirectionalStream.Builder( |
93 null, callback, callback.getExecutor(), mTestFramework.mCron
etEngine); | 115 null, callback, callback.getExecutor(), mTestFramework.mCron
etEngine); |
94 fail("URL not null-checked"); | 116 fail("URL not null-checked"); |
95 } catch (NullPointerException e) { | 117 } catch (NullPointerException e) { |
96 assertEquals("URL is required.", e.getMessage()); | 118 assertEquals("URL is required.", e.getMessage()); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 assertTrue(stream.isDone()); | 178 assertTrue(stream.isDone()); |
157 assertEquals("Exception in BidirectionalStream: net::ERR_DISALLOWED_URL_
SCHEME", | 179 assertEquals("Exception in BidirectionalStream: net::ERR_DISALLOWED_URL_
SCHEME", |
158 callback.mError.getMessage()); | 180 callback.mError.getMessage()); |
159 assertEquals(-301, callback.mError.getCronetInternalErrorCode()); | 181 assertEquals(-301, callback.mError.getCronetInternalErrorCode()); |
160 } | 182 } |
161 | 183 |
162 @SmallTest | 184 @SmallTest |
163 @Feature({"Cronet"}) | 185 @Feature({"Cronet"}) |
164 @OnlyRunNativeCronet | 186 @OnlyRunNativeCronet |
165 public void testSimpleGet() throws Exception { | 187 public void testSimpleGet() throws Exception { |
166 String url = Http2TestServer.getEchoMethodUrl(); | 188 // Since this is the first request on the connection, the expected recei
ved bytes count |
167 TestBidirectionalStreamCallback callback = new TestBidirectionalStreamCa
llback(); | 189 // must account for an HPACK dynamic table size update. |
168 // Create stream. | 190 runSimpleGetWithExpectedReceivedBytesCount(31); |
169 BidirectionalStream stream = new BidirectionalStream | |
170 .Builder(url, callback, callback.ge
tExecutor(), | |
171 mTestFramework.mCronetEngin
e) | |
172 .setHttpMethod("GET") | |
173 .build(); | |
174 stream.start(); | |
175 callback.blockForDone(); | |
176 assertTrue(stream.isDone()); | |
177 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); | |
178 // Default method is 'GET'. | |
179 assertEquals("GET", callback.mResponseAsString); | |
180 UrlResponseInfo urlResponseInfo = | |
181 createUrlResponseInfo(new String[] {url}, "", 200, 27, ":status"
, "200"); | |
182 assertResponseEquals(urlResponseInfo, callback.mResponseInfo); | |
183 checkResponseInfo(callback.mResponseInfo, Http2TestServer.getEchoMethodU
rl(), 200, ""); | |
184 } | 191 } |
185 | 192 |
186 @SmallTest | 193 @SmallTest |
187 @Feature({"Cronet"}) | 194 @Feature({"Cronet"}) |
188 @OnlyRunNativeCronet | 195 @OnlyRunNativeCronet |
189 public void testSimpleHead() throws Exception { | 196 public void testSimpleHead() throws Exception { |
190 String url = Http2TestServer.getEchoMethodUrl(); | 197 String url = Http2TestServer.getEchoMethodUrl(); |
191 TestBidirectionalStreamCallback callback = new TestBidirectionalStreamCa
llback(); | 198 TestBidirectionalStreamCallback callback = new TestBidirectionalStreamCa
llback(); |
192 // Create stream. | 199 // Create stream. |
193 BidirectionalStream stream = new BidirectionalStream | 200 BidirectionalStream stream = new BidirectionalStream |
194 .Builder(url, callback, callback.ge
tExecutor(), | 201 .Builder(url, callback, callback.ge
tExecutor(), |
195 mTestFramework.mCronetEngin
e) | 202 mTestFramework.mCronetEngin
e) |
196 .setHttpMethod("HEAD") | 203 .setHttpMethod("HEAD") |
197 .build(); | 204 .build(); |
198 stream.start(); | 205 stream.start(); |
199 callback.blockForDone(); | 206 callback.blockForDone(); |
200 assertTrue(stream.isDone()); | 207 assertTrue(stream.isDone()); |
201 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); | 208 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
202 assertEquals("HEAD", callback.mResponseAsString); | 209 assertEquals("HEAD", callback.mResponseAsString); |
203 UrlResponseInfo urlResponseInfo = | 210 UrlResponseInfo urlResponseInfo = |
204 createUrlResponseInfo(new String[] {url}, "", 200, 28, ":status"
, "200"); | 211 createUrlResponseInfo(new String[] {url}, "", 200, 32, ":status"
, "200"); |
205 assertResponseEquals(urlResponseInfo, callback.mResponseInfo); | 212 assertResponseEquals(urlResponseInfo, callback.mResponseInfo); |
206 checkResponseInfo(callback.mResponseInfo, Http2TestServer.getEchoMethodU
rl(), 200, ""); | 213 checkResponseInfo(callback.mResponseInfo, Http2TestServer.getEchoMethodU
rl(), 200, ""); |
207 } | 214 } |
208 | 215 |
209 @SmallTest | 216 @SmallTest |
210 @Feature({"Cronet"}) | 217 @Feature({"Cronet"}) |
211 @OnlyRunNativeCronet | 218 @OnlyRunNativeCronet |
212 public void testSimplePost() throws Exception { | 219 public void testSimplePost() throws Exception { |
213 String url = Http2TestServer.getEchoStreamUrl(); | 220 String url = Http2TestServer.getEchoStreamUrl(); |
214 TestBidirectionalStreamCallback callback = new TestBidirectionalStreamCa
llback(); | 221 TestBidirectionalStreamCallback callback = new TestBidirectionalStreamCa
llback(); |
(...skipping 851 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1066 | 1073 |
1067 // Position should not have been modified, since nothing was read. | 1074 // Position should not have been modified, since nothing was read. |
1068 assertEquals(1, readBuffer.position()); | 1075 assertEquals(1, readBuffer.position()); |
1069 // Limit should be unchanged as always. | 1076 // Limit should be unchanged as always. |
1070 assertEquals(5, readBuffer.limit()); | 1077 assertEquals(5, readBuffer.limit()); |
1071 | 1078 |
1072 assertEquals(ResponseStep.ON_SUCCEEDED, callback.mResponseStep); | 1079 assertEquals(ResponseStep.ON_SUCCEEDED, callback.mResponseStep); |
1073 | 1080 |
1074 // Make sure there are no other pending messages, which would trigger | 1081 // Make sure there are no other pending messages, which would trigger |
1075 // asserts in TestBidirectionalCallback. | 1082 // asserts in TestBidirectionalCallback. |
1076 testSimpleGet(); | 1083 // The expected received bytes count is lower than it would be for the f
irst request on the |
| 1084 // connection, because the server includes an HPACK dynamic table size u
pdate only in the |
| 1085 // first response HEADERS frame. |
| 1086 runSimpleGetWithExpectedReceivedBytesCount(27); |
1077 } | 1087 } |
1078 | 1088 |
1079 @SmallTest | 1089 @SmallTest |
1080 @Feature({"Cronet"}) | 1090 @Feature({"Cronet"}) |
1081 @OnlyRunNativeCronet | 1091 @OnlyRunNativeCronet |
1082 public void testBadBuffers() throws Exception { | 1092 public void testBadBuffers() throws Exception { |
1083 TestBidirectionalStreamCallback callback = new TestBidirectionalStreamCa
llback(); | 1093 TestBidirectionalStreamCallback callback = new TestBidirectionalStreamCa
llback(); |
1084 callback.setAutoAdvance(false); | 1094 callback.setAutoAdvance(false); |
1085 BidirectionalStream.Builder builder = | 1095 BidirectionalStream.Builder builder = |
1086 new BidirectionalStream.Builder(Http2TestServer.getEchoMethodUrl
(), callback, | 1096 new BidirectionalStream.Builder(Http2TestServer.getEchoMethodUrl
(), callback, |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1360 private static String bufferContentsToString(ByteBuffer byteBuffer, int star
t, int end) { | 1370 private static String bufferContentsToString(ByteBuffer byteBuffer, int star
t, int end) { |
1361 // Use a duplicate to avoid modifying byteBuffer. | 1371 // Use a duplicate to avoid modifying byteBuffer. |
1362 ByteBuffer duplicate = byteBuffer.duplicate(); | 1372 ByteBuffer duplicate = byteBuffer.duplicate(); |
1363 duplicate.position(start); | 1373 duplicate.position(start); |
1364 duplicate.limit(end); | 1374 duplicate.limit(end); |
1365 byte[] contents = new byte[duplicate.remaining()]; | 1375 byte[] contents = new byte[duplicate.remaining()]; |
1366 duplicate.get(contents); | 1376 duplicate.get(contents); |
1367 return new String(contents); | 1377 return new String(contents); |
1368 } | 1378 } |
1369 } | 1379 } |
OLD | NEW |