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 runSimpleGetWithExpectedReceivedBytesCount(31); |
167 TestBidirectionalStreamCallback callback = new TestBidirectionalStreamCa llback(); | |
168 // Create stream. | |
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 } | 189 } |
185 | 190 |
186 @SmallTest | 191 @SmallTest |
187 @Feature({"Cronet"}) | 192 @Feature({"Cronet"}) |
188 @OnlyRunNativeCronet | 193 @OnlyRunNativeCronet |
189 public void testSimpleHead() throws Exception { | 194 public void testSimpleHead() throws Exception { |
190 String url = Http2TestServer.getEchoMethodUrl(); | 195 String url = Http2TestServer.getEchoMethodUrl(); |
191 TestBidirectionalStreamCallback callback = new TestBidirectionalStreamCa llback(); | 196 TestBidirectionalStreamCallback callback = new TestBidirectionalStreamCa llback(); |
192 // Create stream. | 197 // Create stream. |
193 BidirectionalStream stream = new BidirectionalStream | 198 BidirectionalStream stream = new BidirectionalStream |
194 .Builder(url, callback, callback.ge tExecutor(), | 199 .Builder(url, callback, callback.ge tExecutor(), |
195 mTestFramework.mCronetEngin e) | 200 mTestFramework.mCronetEngin e) |
196 .setHttpMethod("HEAD") | 201 .setHttpMethod("HEAD") |
197 .build(); | 202 .build(); |
198 stream.start(); | 203 stream.start(); |
199 callback.blockForDone(); | 204 callback.blockForDone(); |
200 assertTrue(stream.isDone()); | 205 assertTrue(stream.isDone()); |
201 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); | 206 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
202 assertEquals("HEAD", callback.mResponseAsString); | 207 assertEquals("HEAD", callback.mResponseAsString); |
203 UrlResponseInfo urlResponseInfo = | 208 UrlResponseInfo urlResponseInfo = |
204 createUrlResponseInfo(new String[] {url}, "", 200, 28, ":status" , "200"); | 209 createUrlResponseInfo(new String[] {url}, "", 200, 32, ":status" , "200"); |
205 assertResponseEquals(urlResponseInfo, callback.mResponseInfo); | 210 assertResponseEquals(urlResponseInfo, callback.mResponseInfo); |
206 checkResponseInfo(callback.mResponseInfo, Http2TestServer.getEchoMethodU rl(), 200, ""); | 211 checkResponseInfo(callback.mResponseInfo, Http2TestServer.getEchoMethodU rl(), 200, ""); |
207 } | 212 } |
208 | 213 |
209 @SmallTest | 214 @SmallTest |
210 @Feature({"Cronet"}) | 215 @Feature({"Cronet"}) |
211 @OnlyRunNativeCronet | 216 @OnlyRunNativeCronet |
212 public void testSimplePost() throws Exception { | 217 public void testSimplePost() throws Exception { |
213 String url = Http2TestServer.getEchoStreamUrl(); | 218 String url = Http2TestServer.getEchoStreamUrl(); |
214 TestBidirectionalStreamCallback callback = new TestBidirectionalStreamCa llback(); | 219 TestBidirectionalStreamCallback callback = new TestBidirectionalStreamCa llback(); |
(...skipping 850 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1065 assertEquals("FORTE", bufferContentsToString(readBuffer, 0, 5)); | 1070 assertEquals("FORTE", bufferContentsToString(readBuffer, 0, 5)); |
1066 | 1071 |
1067 // Position should not have been modified, since nothing was read. | 1072 // Position should not have been modified, since nothing was read. |
1068 assertEquals(1, readBuffer.position()); | 1073 assertEquals(1, readBuffer.position()); |
1069 // Limit should be unchanged as always. | 1074 // Limit should be unchanged as always. |
1070 assertEquals(5, readBuffer.limit()); | 1075 assertEquals(5, readBuffer.limit()); |
1071 | 1076 |
1072 assertEquals(ResponseStep.ON_SUCCEEDED, callback.mResponseStep); | 1077 assertEquals(ResponseStep.ON_SUCCEEDED, callback.mResponseStep); |
1073 | 1078 |
1074 // Make sure there are no other pending messages, which would trigger | 1079 // Make sure there are no other pending messages, which would trigger |
1075 // asserts in TestBidirectionalCallback. | 1080 // asserts in TestBidirectionalCallback. |
xunjieli
2016/09/01 19:55:23
Could you add a brief comment here on why we use 2
| |
1076 testSimpleGet(); | 1081 runSimpleGetWithExpectedReceivedBytesCount(27); |
1077 } | 1082 } |
1078 | 1083 |
1079 @SmallTest | 1084 @SmallTest |
1080 @Feature({"Cronet"}) | 1085 @Feature({"Cronet"}) |
1081 @OnlyRunNativeCronet | 1086 @OnlyRunNativeCronet |
1082 public void testBadBuffers() throws Exception { | 1087 public void testBadBuffers() throws Exception { |
1083 TestBidirectionalStreamCallback callback = new TestBidirectionalStreamCa llback(); | 1088 TestBidirectionalStreamCallback callback = new TestBidirectionalStreamCa llback(); |
1084 callback.setAutoAdvance(false); | 1089 callback.setAutoAdvance(false); |
1085 BidirectionalStream.Builder builder = | 1090 BidirectionalStream.Builder builder = |
1086 new BidirectionalStream.Builder(Http2TestServer.getEchoMethodUrl (), callback, | 1091 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) { | 1365 private static String bufferContentsToString(ByteBuffer byteBuffer, int star t, int end) { |
1361 // Use a duplicate to avoid modifying byteBuffer. | 1366 // Use a duplicate to avoid modifying byteBuffer. |
1362 ByteBuffer duplicate = byteBuffer.duplicate(); | 1367 ByteBuffer duplicate = byteBuffer.duplicate(); |
1363 duplicate.position(start); | 1368 duplicate.position(start); |
1364 duplicate.limit(end); | 1369 duplicate.limit(end); |
1365 byte[] contents = new byte[duplicate.remaining()]; | 1370 byte[] contents = new byte[duplicate.remaining()]; |
1366 duplicate.get(contents); | 1371 duplicate.get(contents); |
1367 return new String(contents); | 1372 return new String(contents); |
1368 } | 1373 } |
1369 } | 1374 } |
OLD | NEW |