@Test public void parseNameValueBlock() throws IOException { List<Header> headerBlock = headerEntries( "cache-control", "no-cache, no-store", "set-cookie", "Cookie1\u0000Cookie2", ":status", "200 OK", ":version", "HTTP/1.1"); Request request = new Request.Builder().url("http://square.com/").build(); Response response = Http2xStream.readSpdy3HeadersList(headerBlock).request(request).build(); Headers headers = response.headers(); assertEquals(3, headers.size()); Assert.assertEquals(Protocol.SPDY_3, response.protocol()); assertEquals(200, response.code()); assertEquals("OK", response.message()); assertEquals("no-cache, no-store", headers.get("cache-control")); assertEquals("Cookie2", headers.get("set-cookie")); assertEquals("cache-control", headers.name(0)); assertEquals("no-cache, no-store", headers.value(0)); assertEquals("set-cookie", headers.name(1)); assertEquals("Cookie1", headers.value(1)); assertEquals("set-cookie", headers.name(2)); assertEquals("Cookie2", headers.value(2)); assertNull(headers.get(":status")); assertNull(headers.get(":version")); }
/** * 开始下载任务 * * @param downloadId * @param callback */ public void startTask(int downloadId, FileDownloaderCallback callback) { FileDownloaderModel model = getFileDownloaderModelById(downloadId); if (model != null) { BridgeListener bridgeListener = mListenerManager.getBridgeListener(downloadId); bridgeListener.addDownloadListener(callback); if (mDownloadingList.size() >= mConfiguration.getMaxDownloadingCount()) { // 下载中队列已满 if (!mWaitQueue.contains(model)) { mWaitQueue.offer(model); } bridgeListener.wait(downloadId); } else { mDownloadingList.add(model); final BaseDownloadTask task = FileDownloader.getImpl() .create(model.getUrl()) .setPath(model.getPath()) .setCallbackProgressTimes(100) .setAutoRetryTimes(mAutoRetryTimes) .setListener(bridgeListener); for (int i = 0; i < mHeaders.size(); i++) { task.addHeader(mHeaders.name(i), mHeaders.value(i)); } bridgeListener.setDownloadTask(task); task.start(); } } else { ILogger.e("Task does not exist!"); } }
@Test public void nameIndexesAreStrict() { Headers headers = Headers.of("a", "b", "c", "d"); try { headers.name(-1); fail(); } catch (IndexOutOfBoundsException expected) { } assertEquals("a", headers.name(0)); assertEquals("c", headers.name(1)); try { headers.name(2); fail(); } catch (IndexOutOfBoundsException expected) { } }
@Test public void readNameValueBlockDropsForbiddenHeadersHttp2() throws IOException { List<Header> headerBlock = headerEntries( ":status", "200 OK", ":version", "HTTP/1.1", "connection", "close"); Request request = new Request.Builder().url("http://square.com/").build(); Response response = Http2xStream.readHttp2HeadersList(headerBlock).request(request).build(); Headers headers = response.headers(); assertEquals(1, headers.size()); assertEquals(":version", headers.name(0)); assertEquals("HTTP/1.1", headers.value(0)); }
@Test public void ofMapTrimsKey() { Headers headers = Headers.of(Collections.singletonMap(" User-Agent ", "OkHttp")); assertEquals("User-Agent", headers.name(0)); }
@Test public void ofTrims() { Headers headers = Headers.of("\t User-Agent \n", " \r OkHttp "); assertEquals("User-Agent", headers.name(0)); assertEquals("OkHttp", headers.value(0)); }