Esempio n. 1
0
 @Test
 public void ofMakesDefensiveCopy() {
   String[] namesAndValues = {"User-Agent", "OkHttp"};
   Headers headers = Headers.of(namesAndValues);
   namesAndValues[1] = "Chrome";
   assertEquals("OkHttp", headers.value(0));
 }
 /**
  * 开始下载任务
  *
  * @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!");
   }
 }
Esempio n. 3
0
  @Test
  public void ofMapMakesDefensiveCopy() {
    Map<String, String> namesAndValues = new HashMap<>();
    namesAndValues.put("User-Agent", "OkHttp");

    Headers headers = Headers.of(namesAndValues);
    namesAndValues.put("User-Agent", "Chrome");
    assertEquals("OkHttp", headers.value(0));
  }
Esempio n. 4
0
 @Test
 public void toMultimapGroupsHeaders() {
   Headers headers =
       Headers.of(
           "cache-control", "no-cache",
           "cache-control", "no-store",
           "user-agent", "OkHttp");
   Map<String, List<String>> headerMap = headers.toMultimap();
   assertEquals(2, headerMap.get("cache-control").size());
   assertEquals(1, headerMap.get("user-agent").size());
 }
Esempio n. 5
0
 @Test
 public void readNameValueBlockDropsForbiddenHeadersSpdy3() 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.readSpdy3HeadersList(headerBlock).request(request).build();
   Headers headers = response.headers();
   assertEquals(0, headers.size());
 }
Esempio n. 6
0
 @Test
 public void addParsing() {
   Headers headers =
       new Headers.Builder()
           .add("foo: bar")
           .add(" foo: baz") // Name leading whitespace is trimmed.
           .add("foo : bak") // Name trailing whitespace is trimmed.
           .add("ping:  pong  ") // Value whitespace is trimmed.
           .add("kit:kat") // Space after colon is not required.
           .build();
   assertEquals(Arrays.asList("bar", "baz", "bak"), headers.values("foo"));
   assertEquals(Arrays.asList("pong"), headers.values("ping"));
   assertEquals(Arrays.asList("kat"), headers.values("kit"));
 }
Esempio n. 7
0
 @Test
 public void valueIndexesAreStrict() {
   Headers headers = Headers.of("a", "b", "c", "d");
   try {
     headers.value(-1);
     fail();
   } catch (IndexOutOfBoundsException expected) {
   }
   assertEquals("b", headers.value(0));
   assertEquals("d", headers.value(1));
   try {
     headers.value(2);
     fail();
   } catch (IndexOutOfBoundsException expected) {
   }
 }
    @Override
    protected RequestBody postBody(File file) {
      // 设置请求体
      MultipartBody.Builder builder = new MultipartBody.Builder();
      builder.setType(MultipartBody.FORM);
      // 封装文件请求体
      if (file != null && file.exists()) {
        String filename = file.getName();
        MediaType mediaType = MediaType.parse(guessMimeType(filename));
        RequestBody fileBody = RequestBody.create(mediaType, file);
        builder.addFormDataPart("file", filename, fileBody);
      }
      // 封装请求参数
      HashMap<String, String> params = new HashMap<>();
      addParams(params);
      if (params != null && !params.isEmpty()) {
        for (String key : params.keySet()) {
          builder.addPart(
              Headers.of("Content-Disposition", "form-data; name=\"" + key + "\""),
              RequestBody.create(null, params.get(key)));
        }
      }

      return builder.build();
    }
Esempio n. 9
0
 @Test
 public void ofThrowsOnEmptyName() {
   try {
     Headers.of("", "OkHttp");
     fail();
   } catch (IllegalArgumentException expected) {
   }
 }
Esempio n. 10
0
 @Test
 public void ofThrowsOnNull() {
   try {
     Headers.of("User-Agent", null);
     fail();
   } catch (IllegalArgumentException expected) {
   }
 }
Esempio n. 11
0
 @Test
 public void ofThrowsOddNumberOfHeaders() {
   try {
     Headers.of("User-Agent", "OkHttp", "Content-Length");
     fail();
   } catch (IllegalArgumentException expected) {
   }
 }
Esempio n. 12
0
 @Test
 public void ofRejectsNulChar() {
   try {
     Headers.of("User-Agent", "Square\u0000OkHttp");
     fail();
   } catch (IllegalArgumentException expected) {
   }
 }
Esempio n. 13
0
 @Test
 public void ofMapThrowsOnNull() {
   try {
     Headers.of(Collections.<String, String>singletonMap("User-Agent", null));
     fail();
   } catch (IllegalArgumentException expected) {
   }
 }
Esempio n. 14
0
 @Test
 public void ofMapRejectsNulCharInValue() {
   try {
     Headers.of(Collections.singletonMap("User-\u0000Agent", "OkHttp"));
     fail();
   } catch (IllegalArgumentException expected) {
   }
 }
Esempio n. 15
0
 @Test
 public void ofMapThrowsOnBlankName() {
   try {
     Headers.of(Collections.singletonMap(" ", "OkHttp"));
     fail();
   } catch (IllegalArgumentException expected) {
   }
 }
Esempio n. 16
0
 private void addParams(MultipartBody.Builder builder) {
   if (params != null && !params.isEmpty()) {
     for (String key : params.keySet()) {
       builder.addPart(
           Headers.of("Content-Disposition", "form-data; name=\"" + key + "\""),
           RequestBody.create(null, params.get(key)));
     }
   }
 }
Esempio n. 17
0
  @Deprecated
  private Request buildMultipartFormRequest2(
      String url,
      String[] fileKeys,
      File[] files,
      WeakHashMap<String, String> params,
      Object... tag) {

    MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);

    if (params != null) {
      Iterator<Map.Entry<String, String>> entries = params.entrySet().iterator();
      while (entries.hasNext()) {
        Map.Entry<String, String> entry = entries.next();
        builder.addPart(
            Headers.of("Content-Disposition", "form-data; name=\"" + entry.getKey() + "\""),
            RequestBody.create(null, entry.getValue()));
      }
    }

    if (files != null) {
      RequestBody fileBody = null;
      for (int i = 0; i < files.length; i++) {
        File file = files[i];
        String fileName = file.getName();
        fileBody = RequestBody.create(MediaType.parse(guessMediaType(fileName)), file);
        builder.addPart(
            Headers.of(
                "Content-Disposition",
                "form-data; name=\"" + fileKeys[i] + "\"; filename=\"" + fileName + "\""),
            fileBody);
      }
    }

    RequestBody requestBody = builder.build();
    Request.Builder requestBuilder = new Request.Builder();
    requestBuilder.url(url);
    requestBuilder.post(requestBody);
    if (tag.length > 0) {
      requestBuilder.tag(tag[0]);
    }
    return requestBuilder.build();
  }
Esempio n. 18
0
 public static void setRequestHeader(Map<String, String> headers, HeaderCallBack callBack) {
   if (headers != null) {
     try {
       mHeaders = Headers.of(headers);
     } catch (Exception e) {
       if (callBack != null) {
         callBack.onHeaderError();
       }
       return;
     }
     if (callBack != null) {
       if (mHeaders != null) {
         callBack.onHeaderSuccess();
       }
     }
   }
 }
Esempio n. 19
0
 private static void addFiles(MultipartBody.Builder builder, Pair<String, File>... files) {
   if (files != null) {
     RequestBody fileBody;
     for (int i = 0; i < files.length; i++) {
       Pair<String, File> filePair = files[i];
       String fileKeyName = filePair.first;
       File file = filePair.second;
       String fileName = file.getName();
       fileBody = RequestBody.create(MediaType.parse(guessMimeType(fileName)), file);
       builder.addPart(
           Headers.of(
               "Content-Disposition",
               "form-data; name=\"" + fileKeyName + "\"; filename=\"" + fileName + "\""),
           fileBody);
     }
   } else {
     throw new IllegalArgumentException("File can not be null");
   }
 }
Esempio n. 20
0
 /**
  * Returns the values for the specified header name.
  *
  * @param name the header name.
  * @return the list of values.
  */
 public List<String> headers(String name) {
   return headers.values(name);
 }
Esempio n. 21
0
 /**
  * Sets the headers from a template. All previous headers set are removed.
  *
  * @param headers the headers template.
  * @return this
  */
 public Builder headers(final Headers headers) {
   this.mHeaders = headers.newBuilder();
   return this;
 }
Esempio n. 22
0
 /**
  * Returns the value for the specified header name, or the specified default value is the header
  * isn't set.
  *
  * @param name the header name.
  * @param defaultValue the default value.
  * @return the header name.
  */
 public String header(String name, String defaultValue) {
   String result = headers.get(name);
   return result != null ? result : defaultValue;
 }
Esempio n. 23
0
 @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"));
 }
Esempio n. 24
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));
 }
Esempio n. 25
0
 @Test
 public void ofAcceptsEmptyValue() {
   Headers headers = Headers.of("User-Agent", "");
   assertEquals("", headers.value(0));
 }
Esempio n. 26
0
 @Test
 public void ofMapTrimsValue() {
   Headers headers = Headers.of(Collections.singletonMap("User-Agent", " OkHttp "));
   assertEquals("OkHttp", headers.value(0));
 }
Esempio n. 27
0
 @Test
 public void ofMapAcceptsEmptyValue() {
   Headers headers = Headers.of(Collections.singletonMap("User-Agent", ""));
   assertEquals("", headers.value(0));
 }