From 0072ae443697605d6906eb546606ab0637c23817 Mon Sep 17 00:00:00 2001 From: wangmengyan95 Date: Thu, 10 Sep 2015 17:49:11 -0700 Subject: [PATCH] Move ParseHttpRequset/Response/Body and ParseNetworkInterceptor to separate package and publicize --- .../main/java/com/parse/EventuallyPin.java | 2 + Parse/src/main/java/com/parse/Parse.java | 2 + .../main/java/com/parse/ParseAWSRequest.java | 3 + .../java/com/parse/ParseApacheHttpClient.java | 4 + .../com/parse/ParseByteArrayHttpBody.java | 2 + .../com/parse/ParseDecompressInterceptor.java | 4 + .../java/com/parse/ParseFileController.java | 2 + .../java/com/parse/ParseFileHttpBody.java | 2 + .../main/java/com/parse/ParseHttpClient.java | 4 + .../java/com/parse/ParseHttpResponse.java | 172 ------------ .../java/com/parse/ParseOkHttpClient.java | 4 + .../src/main/java/com/parse/ParsePlugins.java | 4 + .../com/parse/ParseRESTAnalyticsCommand.java | 2 + .../java/com/parse/ParseRESTCloudCommand.java | 2 + .../main/java/com/parse/ParseRESTCommand.java | 4 + .../com/parse/ParseRESTConfigCommand.java | 2 + .../java/com/parse/ParseRESTFileCommand.java | 3 + .../parse/ParseRESTObjectBatchCommand.java | 3 + .../com/parse/ParseRESTObjectCommand.java | 5 +- .../java/com/parse/ParseRESTPushCommand.java | 2 + .../java/com/parse/ParseRESTQueryCommand.java | 2 + .../com/parse/ParseRESTSessionCommand.java | 2 + .../java/com/parse/ParseRESTUserCommand.java | 3 + .../src/main/java/com/parse/ParseRequest.java | 4 + .../parse/ParseURLConnectionHttpClient.java | 4 + .../com/parse/{ => http}/ParseHttpBody.java | 19 +- .../parse/{ => http}/ParseHttpRequest.java | 105 +++++++- .../com/parse/http/ParseHttpResponse.java | 248 ++++++++++++++++++ .../{ => http}/ParseNetworkInterceptor.java | 30 ++- .../parse/NetworkObjectControllerTest.java | 3 + .../java/com/parse/ParseAWSRequestTest.java | 19 +- .../com/parse/ParseApacheHttpClientTest.java | 5 +- .../parse/ParseCloudCodeControllerTest.java | 21 +- .../com/parse/ParseConfigControllerTest.java | 3 + .../parse/ParseDecompressInterceptorTest.java | 4 + .../com/parse/ParseFileControllerTest.java | 36 +-- .../java/com/parse/ParseHttpClientTest.java | 2 + .../java/com/parse/ParseHttpRequestTest.java | 4 +- .../java/com/parse/ParseHttpResponseTest.java | 2 + .../java/com/parse/ParseOkHttpClientTest.java | 3 + .../com/parse/ParsePushControllerTest.java | 3 + .../java/com/parse/ParseRESTCommandTest.java | 34 ++- .../com/parse/ParseRESTQueryCommandTest.java | 2 + .../com/parse/ParseRESTUserCommandTest.java | 3 + .../test/java/com/parse/ParseRequestTest.java | 13 +- .../test/java/com/parse/ParseTestUtils.java | 3 + .../ParseURLConnectionHttpClientTest.java | 4 +- 47 files changed, 566 insertions(+), 243 deletions(-) delete mode 100644 Parse/src/main/java/com/parse/ParseHttpResponse.java rename Parse/src/main/java/com/parse/{ => http}/ParseHttpBody.java (76%) rename Parse/src/main/java/com/parse/{ => http}/ParseHttpRequest.java (54%) create mode 100644 Parse/src/main/java/com/parse/http/ParseHttpResponse.java rename Parse/src/main/java/com/parse/{ => http}/ParseNetworkInterceptor.java (51%) diff --git a/Parse/src/main/java/com/parse/EventuallyPin.java b/Parse/src/main/java/com/parse/EventuallyPin.java index bdfd61c1a..a7b7b2456 100644 --- a/Parse/src/main/java/com/parse/EventuallyPin.java +++ b/Parse/src/main/java/com/parse/EventuallyPin.java @@ -8,6 +8,8 @@ */ package com.parse; +import com.parse.http.ParseHttpRequest; + import org.json.JSONException; import org.json.JSONObject; diff --git a/Parse/src/main/java/com/parse/Parse.java b/Parse/src/main/java/com/parse/Parse.java index 3b215c931..af5391729 100644 --- a/Parse/src/main/java/com/parse/Parse.java +++ b/Parse/src/main/java/com/parse/Parse.java @@ -14,6 +14,8 @@ import android.os.Bundle; import android.util.Log; +import com.parse.http.ParseNetworkInterceptor; + import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; diff --git a/Parse/src/main/java/com/parse/ParseAWSRequest.java b/Parse/src/main/java/com/parse/ParseAWSRequest.java index 19c3b20f5..cca56fee2 100644 --- a/Parse/src/main/java/com/parse/ParseAWSRequest.java +++ b/Parse/src/main/java/com/parse/ParseAWSRequest.java @@ -8,6 +8,9 @@ */ package com.parse; +import com.parse.http.ParseHttpRequest; +import com.parse.http.ParseHttpResponse; + import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; diff --git a/Parse/src/main/java/com/parse/ParseApacheHttpClient.java b/Parse/src/main/java/com/parse/ParseApacheHttpClient.java index 67c4cd9eb..88cbc0755 100644 --- a/Parse/src/main/java/com/parse/ParseApacheHttpClient.java +++ b/Parse/src/main/java/com/parse/ParseApacheHttpClient.java @@ -12,6 +12,10 @@ import android.net.SSLSessionCache; import android.net.http.AndroidHttpClient; +import com.parse.http.ParseHttpBody; +import com.parse.http.ParseHttpRequest; +import com.parse.http.ParseHttpResponse; + import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; diff --git a/Parse/src/main/java/com/parse/ParseByteArrayHttpBody.java b/Parse/src/main/java/com/parse/ParseByteArrayHttpBody.java index ef45be0c3..9642ccb65 100644 --- a/Parse/src/main/java/com/parse/ParseByteArrayHttpBody.java +++ b/Parse/src/main/java/com/parse/ParseByteArrayHttpBody.java @@ -8,6 +8,8 @@ */ package com.parse; +import com.parse.http.ParseHttpBody; + import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; diff --git a/Parse/src/main/java/com/parse/ParseDecompressInterceptor.java b/Parse/src/main/java/com/parse/ParseDecompressInterceptor.java index 01f35a88b..1303d8703 100644 --- a/Parse/src/main/java/com/parse/ParseDecompressInterceptor.java +++ b/Parse/src/main/java/com/parse/ParseDecompressInterceptor.java @@ -9,6 +9,10 @@ package com.parse; +import com.parse.http.ParseHttpRequest; +import com.parse.http.ParseHttpResponse; +import com.parse.http.ParseNetworkInterceptor; + import java.io.IOException; import java.util.HashMap; import java.util.Map; diff --git a/Parse/src/main/java/com/parse/ParseFileController.java b/Parse/src/main/java/com/parse/ParseFileController.java index f5c3ea211..843145650 100644 --- a/Parse/src/main/java/com/parse/ParseFileController.java +++ b/Parse/src/main/java/com/parse/ParseFileController.java @@ -8,6 +8,8 @@ */ package com.parse; +import com.parse.http.ParseHttpRequest; + import org.json.JSONObject; import java.io.File; diff --git a/Parse/src/main/java/com/parse/ParseFileHttpBody.java b/Parse/src/main/java/com/parse/ParseFileHttpBody.java index 010060e3e..378c7d914 100644 --- a/Parse/src/main/java/com/parse/ParseFileHttpBody.java +++ b/Parse/src/main/java/com/parse/ParseFileHttpBody.java @@ -8,6 +8,8 @@ */ package com.parse; +import com.parse.http.ParseHttpBody; + import java.io.File; import java.io.FileInputStream; import java.io.IOException; diff --git a/Parse/src/main/java/com/parse/ParseHttpClient.java b/Parse/src/main/java/com/parse/ParseHttpClient.java index 742d6adeb..1aed73794 100644 --- a/Parse/src/main/java/com/parse/ParseHttpClient.java +++ b/Parse/src/main/java/com/parse/ParseHttpClient.java @@ -11,6 +11,10 @@ import android.net.SSLSessionCache; import android.os.Build; +import com.parse.http.ParseHttpRequest; +import com.parse.http.ParseHttpResponse; +import com.parse.http.ParseNetworkInterceptor; + import java.io.IOException; import java.util.ArrayList; import java.util.List; diff --git a/Parse/src/main/java/com/parse/ParseHttpResponse.java b/Parse/src/main/java/com/parse/ParseHttpResponse.java deleted file mode 100644 index cf3c15ceb..000000000 --- a/Parse/src/main/java/com/parse/ParseHttpResponse.java +++ /dev/null @@ -1,172 +0,0 @@ -/* - * Copyright (c) 2015-present, Parse, LLC. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - */ -package com.parse; - -import java.io.InputStream; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -/** - * The http response we receive from parse server. Instances of this class are not immutable. The - * response body may be consumed only once. The other fields are immutable. - */ -/** package */ class ParseHttpResponse { - - /** - * Base builder for {@code ParseHttpResponse}. - */ - /* package */ static abstract class Init> { - private int statusCode; - private InputStream content; - private long totalSize; - private String reasonPhrase; - private Map headers; - private String contentType; - - /* package */ abstract T self(); - - public Init() { - this.totalSize = -1; - this.headers = new HashMap<>(); - } - - public T setStatusCode(int statusCode) { - this.statusCode = statusCode; - return self(); - } - - public T setContent(InputStream content) { - this.content = content; - return self(); - } - - public T setTotalSize(long totalSize) { - this.totalSize = totalSize; - return self(); - } - - public T setReasonPhrase(String reasonPhrase) { - this.reasonPhrase = reasonPhrase; - return self(); - } - - public T setHeaders(Map headers) { - this.headers = new HashMap<>(headers); - return self(); - } - - public T addHeaders(Map headers) { - this.headers.putAll(headers); - return self(); - } - - public T addHeader(String key, String value) { - headers.put(key, value); - return self(); - } - - public T setContentType(String contentType) { - this.contentType = contentType; - return self(); - } - } - - /** - * Builder of {@code ParseHttpResponse}. - */ - public static class Builder extends Init { - - @Override - /* package */ Builder self() { - return this; - } - - public Builder() { - super(); - } - - /** - * Makes a new {@code ParseHttpResponse} {@code Builder} based on the input - * {@code ParseHttpResponse}. - * - * @param response - * The {@code ParseHttpResponse} where the {@code Builder}'s values come from. - */ - public Builder(ParseHttpResponse response) { - super(); - this.setStatusCode(response.getStatusCode()); - this.setContent(response.getContent()); - this.setTotalSize(response.getTotalSize()); - this.setContentType(response.getContentType()); - this.setHeaders(response.getAllHeaders()); - this.setReasonPhrase(response.getReasonPhrase()); - } - - public ParseHttpResponse build() { - return new ParseHttpResponse(this); - } - } - - private final int statusCode; - private final InputStream content; - private final long totalSize; - private final String reasonPhrase; - private final Map headers; - private final String contentType; - - /* package */ ParseHttpResponse(Init builder) { - this.statusCode = builder.statusCode; - this.content = builder.content; - this.totalSize = builder.totalSize; - this.reasonPhrase = builder.reasonPhrase; - this.headers = Collections.unmodifiableMap(new HashMap<>(builder.headers)); - this.contentType = builder.contentType; - } - - public int getStatusCode() { - return statusCode; - } - - /** - * Returns the content of the {@code ParseHttpResponse}'s body. The {@link InputStream} can only - * be read once and can't be reset. - * - * @return The {@link InputStream} of the {@code ParseHttpResponse}'s body. - */ - public InputStream getContent() { - return content; - } - - /** - * Returns the size of the {@code ParseHttpResponse}'s body. -1 if the size of the - * {@code ParseHttpResponse}'s body is unknown. - * - * @return The size of the {@code ParseHttpResponse}'s body. - */ - public long getTotalSize() { - return totalSize; - } - - public String getReasonPhrase() { - return reasonPhrase; - } - - public String getContentType() { - return contentType; - } - - public String getHeader(String name) { - return headers.get(name); - } - - public Map getAllHeaders() { - return headers; - } -} diff --git a/Parse/src/main/java/com/parse/ParseOkHttpClient.java b/Parse/src/main/java/com/parse/ParseOkHttpClient.java index f45b650a7..fa2ec2eea 100644 --- a/Parse/src/main/java/com/parse/ParseOkHttpClient.java +++ b/Parse/src/main/java/com/parse/ParseOkHttpClient.java @@ -11,6 +11,10 @@ import android.net.SSLCertificateSocketFactory; import android.net.SSLSessionCache; +import com.parse.http.ParseHttpBody; +import com.parse.http.ParseHttpRequest; +import com.parse.http.ParseHttpResponse; +import com.parse.http.ParseNetworkInterceptor; import com.squareup.okhttp.Call; import com.squareup.okhttp.Headers; import com.squareup.okhttp.Interceptor; diff --git a/Parse/src/main/java/com/parse/ParsePlugins.java b/Parse/src/main/java/com/parse/ParsePlugins.java index 65a839fdb..d15515e27 100644 --- a/Parse/src/main/java/com/parse/ParsePlugins.java +++ b/Parse/src/main/java/com/parse/ParsePlugins.java @@ -13,6 +13,10 @@ import android.net.SSLSessionCache; import android.os.Build; +import com.parse.http.ParseHttpRequest; +import com.parse.http.ParseHttpResponse; +import com.parse.http.ParseNetworkInterceptor; + import java.io.File; import java.io.IOException; diff --git a/Parse/src/main/java/com/parse/ParseRESTAnalyticsCommand.java b/Parse/src/main/java/com/parse/ParseRESTAnalyticsCommand.java index 335cf0bb8..95c933de2 100644 --- a/Parse/src/main/java/com/parse/ParseRESTAnalyticsCommand.java +++ b/Parse/src/main/java/com/parse/ParseRESTAnalyticsCommand.java @@ -10,6 +10,8 @@ import android.net.Uri; +import com.parse.http.ParseHttpRequest; + import org.json.JSONObject; import java.util.Date; diff --git a/Parse/src/main/java/com/parse/ParseRESTCloudCommand.java b/Parse/src/main/java/com/parse/ParseRESTCloudCommand.java index 988a27507..6439d0545 100644 --- a/Parse/src/main/java/com/parse/ParseRESTCloudCommand.java +++ b/Parse/src/main/java/com/parse/ParseRESTCloudCommand.java @@ -8,6 +8,8 @@ */ package com.parse; +import com.parse.http.ParseHttpRequest; + import java.util.Map; /** package */ class ParseRESTCloudCommand extends ParseRESTCommand { diff --git a/Parse/src/main/java/com/parse/ParseRESTCommand.java b/Parse/src/main/java/com/parse/ParseRESTCommand.java index 4308016cd..47ae77d18 100644 --- a/Parse/src/main/java/com/parse/ParseRESTCommand.java +++ b/Parse/src/main/java/com/parse/ParseRESTCommand.java @@ -8,6 +8,10 @@ */ package com.parse; +import com.parse.http.ParseHttpBody; +import com.parse.http.ParseHttpRequest; +import com.parse.http.ParseHttpResponse; + import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; diff --git a/Parse/src/main/java/com/parse/ParseRESTConfigCommand.java b/Parse/src/main/java/com/parse/ParseRESTConfigCommand.java index bfb5e24a7..2b280e8b6 100644 --- a/Parse/src/main/java/com/parse/ParseRESTConfigCommand.java +++ b/Parse/src/main/java/com/parse/ParseRESTConfigCommand.java @@ -8,6 +8,8 @@ */ package com.parse; +import com.parse.http.ParseHttpRequest; + import java.util.HashMap; import java.util.Map; diff --git a/Parse/src/main/java/com/parse/ParseRESTFileCommand.java b/Parse/src/main/java/com/parse/ParseRESTFileCommand.java index f6d6a45c9..9ced36925 100644 --- a/Parse/src/main/java/com/parse/ParseRESTFileCommand.java +++ b/Parse/src/main/java/com/parse/ParseRESTFileCommand.java @@ -12,6 +12,9 @@ * REST network command for creating & uploading {@link ParseFile}s. */ +import com.parse.http.ParseHttpBody; +import com.parse.http.ParseHttpRequest; + import java.io.File; /** package */ class ParseRESTFileCommand extends ParseRESTCommand { diff --git a/Parse/src/main/java/com/parse/ParseRESTObjectBatchCommand.java b/Parse/src/main/java/com/parse/ParseRESTObjectBatchCommand.java index ece3e864b..99522eecb 100644 --- a/Parse/src/main/java/com/parse/ParseRESTObjectBatchCommand.java +++ b/Parse/src/main/java/com/parse/ParseRESTObjectBatchCommand.java @@ -8,6 +8,9 @@ */ package com.parse; +import com.parse.http.ParseHttpRequest; +import com.parse.http.ParseHttpResponse; + import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; diff --git a/Parse/src/main/java/com/parse/ParseRESTObjectCommand.java b/Parse/src/main/java/com/parse/ParseRESTObjectCommand.java index 80c32a7b8..31fbd6110 100644 --- a/Parse/src/main/java/com/parse/ParseRESTObjectCommand.java +++ b/Parse/src/main/java/com/parse/ParseRESTObjectCommand.java @@ -10,10 +10,9 @@ import android.net.Uri; -import org.json.JSONObject; +import com.parse.http.ParseHttpRequest; -import java.net.URI; -import java.util.Map; +import org.json.JSONObject; /** package */ class ParseRESTObjectCommand extends ParseRESTCommand { diff --git a/Parse/src/main/java/com/parse/ParseRESTPushCommand.java b/Parse/src/main/java/com/parse/ParseRESTPushCommand.java index 032f47066..9a1993c37 100644 --- a/Parse/src/main/java/com/parse/ParseRESTPushCommand.java +++ b/Parse/src/main/java/com/parse/ParseRESTPushCommand.java @@ -8,6 +8,8 @@ */ package com.parse; +import com.parse.http.ParseHttpRequest; + import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; diff --git a/Parse/src/main/java/com/parse/ParseRESTQueryCommand.java b/Parse/src/main/java/com/parse/ParseRESTQueryCommand.java index f73ad8326..b06cef09a 100644 --- a/Parse/src/main/java/com/parse/ParseRESTQueryCommand.java +++ b/Parse/src/main/java/com/parse/ParseRESTQueryCommand.java @@ -8,6 +8,8 @@ */ package com.parse; +import com.parse.http.ParseHttpRequest; + import org.json.JSONObject; import java.util.HashMap; diff --git a/Parse/src/main/java/com/parse/ParseRESTSessionCommand.java b/Parse/src/main/java/com/parse/ParseRESTSessionCommand.java index e30641353..1472528ee 100644 --- a/Parse/src/main/java/com/parse/ParseRESTSessionCommand.java +++ b/Parse/src/main/java/com/parse/ParseRESTSessionCommand.java @@ -8,6 +8,8 @@ */ package com.parse; +import com.parse.http.ParseHttpRequest; + import org.json.JSONObject; /** package */ class ParseRESTSessionCommand extends ParseRESTCommand { diff --git a/Parse/src/main/java/com/parse/ParseRESTUserCommand.java b/Parse/src/main/java/com/parse/ParseRESTUserCommand.java index e60a73635..0ef50aba2 100644 --- a/Parse/src/main/java/com/parse/ParseRESTUserCommand.java +++ b/Parse/src/main/java/com/parse/ParseRESTUserCommand.java @@ -8,6 +8,9 @@ */ package com.parse; +import com.parse.http.ParseHttpRequest; +import com.parse.http.ParseHttpResponse; + import org.json.JSONException; import org.json.JSONObject; diff --git a/Parse/src/main/java/com/parse/ParseRequest.java b/Parse/src/main/java/com/parse/ParseRequest.java index 7ec2d7b0a..b1fef304b 100644 --- a/Parse/src/main/java/com/parse/ParseRequest.java +++ b/Parse/src/main/java/com/parse/ParseRequest.java @@ -10,6 +10,10 @@ import android.os.Build; +import com.parse.http.ParseHttpBody; +import com.parse.http.ParseHttpRequest; +import com.parse.http.ParseHttpResponse; + import java.io.IOException; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ExecutorService; diff --git a/Parse/src/main/java/com/parse/ParseURLConnectionHttpClient.java b/Parse/src/main/java/com/parse/ParseURLConnectionHttpClient.java index dca7cc5bd..48c720a0a 100644 --- a/Parse/src/main/java/com/parse/ParseURLConnectionHttpClient.java +++ b/Parse/src/main/java/com/parse/ParseURLConnectionHttpClient.java @@ -11,6 +11,10 @@ import android.net.SSLCertificateSocketFactory; import android.net.SSLSessionCache; +import com.parse.http.ParseHttpBody; +import com.parse.http.ParseHttpRequest; +import com.parse.http.ParseHttpResponse; + import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; diff --git a/Parse/src/main/java/com/parse/ParseHttpBody.java b/Parse/src/main/java/com/parse/http/ParseHttpBody.java similarity index 76% rename from Parse/src/main/java/com/parse/ParseHttpBody.java rename to Parse/src/main/java/com/parse/http/ParseHttpBody.java index 69fca5e04..36dba2861 100644 --- a/Parse/src/main/java/com/parse/ParseHttpBody.java +++ b/Parse/src/main/java/com/parse/http/ParseHttpBody.java @@ -6,7 +6,7 @@ * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. */ -package com.parse; +package com.parse.http; import java.io.IOException; import java.io.InputStream; @@ -16,7 +16,8 @@ * The base interface of a http body. It can be implemented by different http libraries such as * Apache http, Android URLConnection, Square OKHttp and so on. */ -/** package */ abstract class ParseHttpBody { +public abstract class ParseHttpBody { + protected final String contentType; protected final long contentLength; @@ -39,6 +40,14 @@ */ public abstract void writeTo(OutputStream out) throws IOException; + /** + * Creates an {@code ParseHttpBody} with given {@code Content-Type} and {@code Content-Length}. + * + * @param contentType + * The {@code Content-Type} of the {@code ParseHttpBody}. + * @param contentLength + * The {@code Content-Length} of the {@code ParseHttpBody}. + */ public ParseHttpBody(String contentType, long contentLength) { this.contentType = contentType; this.contentLength = contentLength; @@ -46,7 +55,7 @@ public ParseHttpBody(String contentType, long contentLength) { /** * Returns the number of bytes which will be written to {@code out} when {@link #writeTo} is - * called, or -1 if that count is unknown. + * called, or {@code -1} if that count is unknown. * * @return The Content-Length of this body. */ @@ -55,9 +64,9 @@ public long getContentLength() { } /** - * Returns the Content-Type of this body. + * Returns the {@code Content-Type} of this body. * - * @return The Content-Type of this body. + * @return The {@code Content-Type} of this body. */ public String getContentType() { return contentType; diff --git a/Parse/src/main/java/com/parse/ParseHttpRequest.java b/Parse/src/main/java/com/parse/http/ParseHttpRequest.java similarity index 54% rename from Parse/src/main/java/com/parse/ParseHttpRequest.java rename to Parse/src/main/java/com/parse/http/ParseHttpRequest.java index dd225326b..1105091f9 100644 --- a/Parse/src/main/java/com/parse/ParseHttpRequest.java +++ b/Parse/src/main/java/com/parse/http/ParseHttpRequest.java @@ -6,7 +6,7 @@ * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. */ -package com.parse; +package com.parse.http; import java.util.Collections; import java.util.HashMap; @@ -16,14 +16,23 @@ * The http request we send to parse server. Instances of this class are not immutable. The * request body may be consumed only once. The other fields are immutable. */ -/** package */ class ParseHttpRequest { +public final class ParseHttpRequest { /** * The {@code ParseHttpRequest} method type. */ public enum Method { + GET, POST, PUT, DELETE; + /** + * Creates a {@code Method} from the given string. Valid stings are {@code GET}, {@code POST}, + * {@code PUT} and {@code DELETE}. + * + * @param string + * The string value of this {@code Method}. + * @return A {@code Method} based on the given string. + */ public static Method fromString(String string) { Method method; switch (string) { @@ -45,6 +54,10 @@ public static Method fromString(String string) { return method; } + /** + * Returns a string value of this {@code Method}. + * @return The string value of this {@code Method}. + */ @Override public String toString() { String string; @@ -71,16 +84,26 @@ public String toString() { /** * Builder of {@code ParseHttpRequest}. */ - public static class Builder { + public static final class Builder { + private String url; private Method method; private Map headers; private ParseHttpBody body; + /** + * Creates an empty {@code Builder}. + */ public Builder() { this.headers = new HashMap<>(); } + /** + * Creates a new {@Builder} based on the given {@code ParseHttpRequest}. + * + * @param request + * The {@code ParseHttpRequest} where the {@code Builder}'s values come from. + */ public Builder(ParseHttpRequest request) { this.url = request.url; this.method = request.method; @@ -88,36 +111,85 @@ public Builder(ParseHttpRequest request) { this.body = request.body; } + /** + * Sets the url of this {@code Builder}. + * + * @param url + * The url of this {@code Builder}. + * @return This {@code Builder}. + */ public Builder setUrl(String url) { this.url = url; return this; } + /** + * Sets the {@link com.parse.http.ParseHttpRequest.Method} of this {@code Builder}. + * + * @param method + * The {@link com.parse.http.ParseHttpRequest.Method} of this {@code Builder}. + * @return This {@code Builder}. + */ public Builder setMethod(ParseHttpRequest.Method method) { this.method = method; return this; } + /** + * Sets the {@link ParseHttpBody} of this {@code Builder}. + * + * @param body + * The {@link ParseHttpBody} of this {@code Builder}. + * @return This {@code Builder}. + */ public Builder setBody(ParseHttpBody body) { this.body = body; return this; } + /** + * Adds a header to this {@code Builder}. + * + * @param name + * The name of the header. + * @param value + * The value of the header. + * @return This {@code Builder}. + */ public Builder addHeader(String name, String value) { headers.put(name, value); return this; } + /** + * Adds headers to this {@code Builder}. + * + * @param headers + * The headers that need to be added. + * @return This {@code Builder}. + */ public Builder addHeaders(Map headers) { this.headers.putAll(headers); return this; } + /** + * Sets headers of this {@code Builder}. All existing headers will be cleared. + * + * @param headers + * The headers of this {@code Builder}. + * @return This {@code Builder}. + */ public Builder setHeaders(Map headers) { this.headers = new HashMap<>(headers); return this; } + /** + * Builds a {@link ParseHttpRequest} based on this {@code Builder}. + * + * @return A {@link ParseHttpRequest} built on this {@code Builder}. + */ public ParseHttpRequest build() { return new ParseHttpRequest(this); } @@ -135,22 +207,49 @@ private ParseHttpRequest(Builder builder) { this.body = builder.body; } + /** + * Gets the url of this {@code ParseHttpRequest}. + * + * @return The url of this {@code ParseHttpRequest}. + */ public String getUrl() { return url; } + /** + * Gets the {@code Method} of this {@code ParseHttpRequest}. + * + * @return The {@code Method} of this {@code ParseHttpRequest}. + */ public Method getMethod() { return method; } + /** + * Gets all headers from this {@code ParseHttpRequest}. + * + * @return The headers of this {@code ParseHttpRequest}. + */ public Map getAllHeaders() { return headers; } + /** + * Retrieves the header value from this {@code ParseHttpRequest} by the given header name. + * + * @param name + * The name of the header. + * @return The value of the header. + */ public String getHeader(String name) { return headers.get(name); } + /** + * Gets http body of this {@code ParseHttpRequest}. + * + * @return The http body of this {@code ParseHttpRequest}. + */ public ParseHttpBody getBody() { return body; } diff --git a/Parse/src/main/java/com/parse/http/ParseHttpResponse.java b/Parse/src/main/java/com/parse/http/ParseHttpResponse.java new file mode 100644 index 000000000..cf402fb0c --- /dev/null +++ b/Parse/src/main/java/com/parse/http/ParseHttpResponse.java @@ -0,0 +1,248 @@ +/* + * Copyright (c) 2015-present, Parse, LLC. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ +package com.parse.http; + +import java.io.InputStream; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +/** + * The http response we receive from parse server. Instances of this class are not immutable. The + * response body may be consumed only once. The other fields are immutable. + */ +public final class ParseHttpResponse { + + /** + * Builder for {@code ParseHttpResponse}. + */ + public static final class Builder { + + private int statusCode; + private InputStream content; + private long totalSize; + private String reasonPhrase; + private Map headers; + private String contentType; + + /** + * Creates an empty {@code Builder}. + */ + public Builder() { + this.totalSize = -1; + this.headers = new HashMap<>(); + } + + /** + * Makes a new {@code Builder} based on the given {@code ParseHttpResponse}. + * + * @param response + * The {@code ParseHttpResponse} where the {@code Builder}'s values come from. + */ + public Builder(ParseHttpResponse response) { + super(); + this.setStatusCode(response.getStatusCode()); + this.setContent(response.getContent()); + this.setTotalSize(response.getTotalSize()); + this.setContentType(response.getContentType()); + this.setHeaders(response.getAllHeaders()); + this.setReasonPhrase(response.getReasonPhrase()); + } + + /** + * Sets the status code of this {@code Builder}. + * + * @param statusCode + * The status code of this {@code Builder}. + * @return This {@code Builder}. + */ + public Builder setStatusCode(int statusCode) { + this.statusCode = statusCode; + return this; + } + + /** + * Sets the content of this {@code Builder}. + * + * @param content + * The content of this {@code Builder}. + * @return This {@code Builder}. + */ + public Builder setContent(InputStream content) { + this.content = content; + return this; + } + + /** + * Sets the total size of this {@code Builder}. + * + * @param totalSize + * The total size of this {@code Builder}. + * @return This {@code Builder}. + */ + public Builder setTotalSize(long totalSize) { + this.totalSize = totalSize; + return this; + } + + /** + * Sets the reason phrase of this {@code Builder}. + * + * @param reasonPhrase + * The reason phrase of this {@code Builder}. + * @return This {@code Builder}. + */ + public Builder setReasonPhrase(String reasonPhrase) { + this.reasonPhrase = reasonPhrase; + return this; + } + + /** + * Sets headers of this {@code Builder}. All existing headers will be cleared. + * + * @param headers + * The headers of this {@code Builder}. + * @return This {@code Builder}. + */ + public Builder setHeaders(Map headers) { + this.headers = new HashMap<>(headers); + return this; + } + + /** + * Adds headers to this {@code Builder}. + * + * @param headers + * The headers that need to be added. + * @return This {@code Builder}. + */ + public Builder addHeaders(Map headers) { + this.headers.putAll(headers); + return this; + } + + /** + * Adds a header to this {@code Builder}. + * + * @param name + * The name of the header. + * @param value + * The value of the header. + * @return This {@code Builder}. + */ + public Builder addHeader(String name, String value) { + headers.put(name, value); + return this; + } + + /** + * Sets the content type of this {@code Builder}. + * + * @param contentType + * The {@code Content-Type} of this {@code Builder}. + * @return This {@code Builder}. + */ + public Builder setContentType(String contentType) { + this.contentType = contentType; + return this; + } + + /** + * Builds a {@link ParseHttpResponse} by this {@code Builder}. + * + * @return A {@link ParseHttpResponse} built on this {@code Builder}. + */ + public ParseHttpResponse build() { + return new ParseHttpResponse(this); + } + } + + private final int statusCode; + private final InputStream content; + private final long totalSize; + private final String reasonPhrase; + private final Map headers; + private final String contentType; + + private ParseHttpResponse(Builder builder) { + this.statusCode = builder.statusCode; + this.content = builder.content; + this.totalSize = builder.totalSize; + this.reasonPhrase = builder.reasonPhrase; + this.headers = Collections.unmodifiableMap(new HashMap<>(builder.headers)); + this.contentType = builder.contentType; + } + + /** + * Gets the status code of this {@code ParseHttpResponse}. + * + * @return The status code of this {@code ParseHttpResponse}. + */ + public int getStatusCode() { + return statusCode; + } + + /** + * Returns the content of the {@code ParseHttpResponse}'s body. The content can only + * be read once and can't be reset. + * + * @return The content of the {@code ParseHttpResponse}'s body. + */ + public InputStream getContent() { + return content; + } + + /** + * Returns the size of the {@code ParseHttpResponse}'s body. {@code -1} if the size of the + * {@code ParseHttpResponse}'s body is unknown. + * + * @return The size of the {@code ParseHttpResponse}'s body. + */ + public long getTotalSize() { + return totalSize; + } + + /** + * Gets the reason phrase of this {@code ParseHttpResponse}. + * + * @return The reason phrase of this {@code ParseHttpResponse}. + */ + public String getReasonPhrase() { + return reasonPhrase; + } + + /** + * Gets the {@code Content-Type} of this {@code ParseHttpResponse}. + * + * @return The {@code Content-Type} of this {@code ParseHttpResponse}. + */ + public String getContentType() { + return contentType; + } + + /** + * Retrieves the header value from this {@code ParseHttpResponse} by the given header name. + * + * @param name + * The name of the header. + * @return The value of the header. + */ + public String getHeader(String name) { + return headers.get(name); + } + + /** + * Gets all headers from this {@code ParseHttpResponse}. + * + * @return The headers of this {@code ParseHttpResponse}. + */ + public Map getAllHeaders() { + return headers; + } +} diff --git a/Parse/src/main/java/com/parse/ParseNetworkInterceptor.java b/Parse/src/main/java/com/parse/http/ParseNetworkInterceptor.java similarity index 51% rename from Parse/src/main/java/com/parse/ParseNetworkInterceptor.java rename to Parse/src/main/java/com/parse/http/ParseNetworkInterceptor.java index 0491b7272..7d163079f 100644 --- a/Parse/src/main/java/com/parse/ParseNetworkInterceptor.java +++ b/Parse/src/main/java/com/parse/http/ParseNetworkInterceptor.java @@ -6,19 +6,23 @@ * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. */ -package com.parse; +package com.parse.http; import java.io.IOException; /** - * {@code ParseNetworkInterceptor} is used to observe requests going out and the corresponding responses coming - * back in. + * {@code ParseNetworkInterceptor} is used to observe requests going out and the corresponding + * responses coming back in. */ -/** package */ interface ParseNetworkInterceptor { +public interface ParseNetworkInterceptor { /** + * Intercepts a {@link ParseHttpRequest} with the help of + * {@link com.parse.http.ParseNetworkInterceptor.Chain} and returns the intercepted + * {@link ParseHttpResponse}. + * * @param chain - * The helper chain we used to get the request, proceed the request and receive the + * The helper chain we use to get the request, proceed the request and receive the * response. * @return The intercepted response. * @throws IOException @@ -31,7 +35,23 @@ * interceptor. In most of the cases, you don't need to implement this interface. */ interface Chain { + + /** + * Gets the {@link ParseHttpRequest} from this chain. + * + * @return The {@link ParseHttpRequest} of this chain. + */ ParseHttpRequest getRequest(); + + /** + * Proceeds the intercepted {@link ParseHttpRequest} in this chain to next + * {@code ParseNetworkInterceptor} or network and gets the {@link ParseHttpResponse}. + * + * @param request + * The intercepted {@link ParseHttpRequest}. + * @return The {@link ParseHttpResponse} from next {@code ParseNetworkInterceptor} or network. + * @throws IOException + */ ParseHttpResponse proceed(ParseHttpRequest request) throws IOException; } } diff --git a/Parse/src/test/java/com/parse/NetworkObjectControllerTest.java b/Parse/src/test/java/com/parse/NetworkObjectControllerTest.java index 3eaeb3854..3af0f81e9 100644 --- a/Parse/src/test/java/com/parse/NetworkObjectControllerTest.java +++ b/Parse/src/test/java/com/parse/NetworkObjectControllerTest.java @@ -8,6 +8,9 @@ */ package com.parse; +import com.parse.http.ParseHttpRequest; +import com.parse.http.ParseHttpResponse; + import org.json.JSONArray; import org.json.JSONObject; import org.junit.Test; diff --git a/Parse/src/test/java/com/parse/ParseAWSRequestTest.java b/Parse/src/test/java/com/parse/ParseAWSRequestTest.java index 6f20fe204..21e7c5ef9 100644 --- a/Parse/src/test/java/com/parse/ParseAWSRequestTest.java +++ b/Parse/src/test/java/com/parse/ParseAWSRequestTest.java @@ -8,14 +8,12 @@ */ package com.parse; -import junit.framework.TestCase; +import com.parse.http.ParseHttpRequest; +import com.parse.http.ParseHttpResponse; -import org.junit.Rule; -import org.junit.rules.ExpectedException; -import org.junit.rules.TemporaryFolder; +import junit.framework.TestCase; import java.io.ByteArrayInputStream; -import java.io.File; import java.io.InputStream; import bolts.Task; @@ -37,11 +35,12 @@ public void test4XXThrowsException() throws Exception { ParseRequest.setDefaultInitialRetryDelay(1L); InputStream mockInputStream = new ByteArrayInputStream( "An Error occurred while saving".getBytes()); - ParseHttpResponse mockResponse = mock(ParseHttpResponse.class); - when(mockResponse.getStatusCode()).thenReturn(400); - when(mockResponse.getTotalSize()).thenReturn(0L); - when(mockResponse.getReasonPhrase()).thenReturn("Bad Request"); - when(mockResponse.getContent()).thenReturn(mockInputStream); + ParseHttpResponse mockResponse = new ParseHttpResponse.Builder() + .setStatusCode(400) + .setTotalSize(0L) + .setReasonPhrase("Bad Request") + .setContent(mockInputStream) + .build(); ParseHttpClient mockHttpClient = mock(ParseHttpClient.class); when(mockHttpClient.execute(any(ParseHttpRequest.class))).thenReturn(mockResponse); diff --git a/Parse/src/test/java/com/parse/ParseApacheHttpClientTest.java b/Parse/src/test/java/com/parse/ParseApacheHttpClientTest.java index 1ece3c225..d97a7a3f6 100644 --- a/Parse/src/test/java/com/parse/ParseApacheHttpClientTest.java +++ b/Parse/src/test/java/com/parse/ParseApacheHttpClientTest.java @@ -8,6 +8,9 @@ */ package com.parse; +import com.parse.http.ParseHttpRequest; +import com.parse.http.ParseHttpResponse; + import org.apache.http.ProtocolVersion; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; @@ -19,7 +22,6 @@ import org.apache.http.message.BasicStatusLine; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Mockito; import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; @@ -31,7 +33,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; // For android.net.SSLCertificateSocketFactory diff --git a/Parse/src/test/java/com/parse/ParseCloudCodeControllerTest.java b/Parse/src/test/java/com/parse/ParseCloudCodeControllerTest.java index 9770b870e..a6f25f7fa 100644 --- a/Parse/src/test/java/com/parse/ParseCloudCodeControllerTest.java +++ b/Parse/src/test/java/com/parse/ParseCloudCodeControllerTest.java @@ -8,6 +8,9 @@ */ package com.parse; +import com.parse.http.ParseHttpRequest; +import com.parse.http.ParseHttpResponse; + import org.json.JSONArray; import org.json.JSONObject; import org.junit.Test; @@ -104,10 +107,11 @@ public void testCallFunctionInBackgroundSuccessWithResult() throws Exception { json.put("result", "test"); String content = json.toString(); - ParseHttpResponse mockResponse = mock(ParseHttpResponse.class); - when(mockResponse.getStatusCode()).thenReturn(200); - when(mockResponse.getContent()).thenReturn(new ByteArrayInputStream(content.getBytes())); - when(mockResponse.getTotalSize()).thenReturn((long) content.length()); + ParseHttpResponse mockResponse = new ParseHttpResponse.Builder() + .setStatusCode(200) + .setTotalSize((long) content.length()) + .setContent(new ByteArrayInputStream(content.getBytes())) + .build(); ParseHttpClient restClient = mockParseHttpClientWithReponse(mockResponse); ParseCloudCodeController controller = new ParseCloudCodeController(restClient); @@ -125,10 +129,11 @@ public void testCallFunctionInBackgroundSuccessWithoutResult() throws Exception JSONObject json = new JSONObject(); String content = json.toString(); - ParseHttpResponse mockResponse = mock(ParseHttpResponse.class); - when(mockResponse.getStatusCode()).thenReturn(200); - when(mockResponse.getContent()).thenReturn(new ByteArrayInputStream(content.getBytes())); - when(mockResponse.getTotalSize()).thenReturn((long) content.length()); + ParseHttpResponse mockResponse = new ParseHttpResponse.Builder() + .setStatusCode(200) + .setTotalSize((long) content.length()) + .setContent(new ByteArrayInputStream(content.getBytes())) + .build(); ParseHttpClient restClient = mockParseHttpClientWithReponse(mockResponse); ParseCloudCodeController controller = new ParseCloudCodeController(restClient); diff --git a/Parse/src/test/java/com/parse/ParseConfigControllerTest.java b/Parse/src/test/java/com/parse/ParseConfigControllerTest.java index c393cfc44..8d7b5b4f2 100644 --- a/Parse/src/test/java/com/parse/ParseConfigControllerTest.java +++ b/Parse/src/test/java/com/parse/ParseConfigControllerTest.java @@ -8,6 +8,9 @@ */ package com.parse; +import com.parse.http.ParseHttpRequest; +import com.parse.http.ParseHttpResponse; + import org.json.JSONObject; import org.junit.Rule; import org.junit.Test; diff --git a/Parse/src/test/java/com/parse/ParseDecompressInterceptorTest.java b/Parse/src/test/java/com/parse/ParseDecompressInterceptorTest.java index a412b719c..1482bb104 100644 --- a/Parse/src/test/java/com/parse/ParseDecompressInterceptorTest.java +++ b/Parse/src/test/java/com/parse/ParseDecompressInterceptorTest.java @@ -9,6 +9,10 @@ package com.parse; +import com.parse.http.ParseHttpRequest; +import com.parse.http.ParseHttpResponse; +import com.parse.http.ParseNetworkInterceptor; + import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricGradleTestRunner; diff --git a/Parse/src/test/java/com/parse/ParseFileControllerTest.java b/Parse/src/test/java/com/parse/ParseFileControllerTest.java index 76409b285..76badef88 100644 --- a/Parse/src/test/java/com/parse/ParseFileControllerTest.java +++ b/Parse/src/test/java/com/parse/ParseFileControllerTest.java @@ -8,6 +8,9 @@ */ package com.parse; +import com.parse.http.ParseHttpRequest; +import com.parse.http.ParseHttpResponse; + import org.json.JSONObject; import org.junit.After; import org.junit.Rule; @@ -127,13 +130,14 @@ public void testSaveAsyncSuccessWithByteArray() throws Exception { json.put("url", "http://example.com"); String content = json.toString(); - ParseHttpResponse response = mock(ParseHttpResponse.class); - when(response.getStatusCode()).thenReturn(200); - when(response.getContent()).thenReturn(new ByteArrayInputStream(content.getBytes())); - when(response.getTotalSize()).thenReturn((long) content.length()); + ParseHttpResponse mockResponse = new ParseHttpResponse.Builder() + .setStatusCode(200) + .setTotalSize((long) content.length()) + .setContent(new ByteArrayInputStream(content.getBytes())) + .build(); ParseHttpClient restClient = mock(ParseHttpClient.class); - when(restClient.execute(any(ParseHttpRequest.class))).thenReturn(response); + when(restClient.execute(any(ParseHttpRequest.class))).thenReturn(mockResponse); File root = temporaryFolder.getRoot(); ParseFileController controller = new ParseFileController(restClient, root); @@ -161,13 +165,14 @@ public void testSaveAsyncSuccessWithFile() throws Exception { json.put("url", "http://example.com"); String content = json.toString(); - ParseHttpResponse response = mock(ParseHttpResponse.class); - when(response.getStatusCode()).thenReturn(200); - when(response.getContent()).thenReturn(new ByteArrayInputStream(content.getBytes())); - when(response.getTotalSize()).thenReturn((long) content.length()); + ParseHttpResponse mockResponse = new ParseHttpResponse.Builder() + .setStatusCode(200) + .setTotalSize((long) content.length()) + .setContent(new ByteArrayInputStream(content.getBytes())) + .build(); ParseHttpClient restClient = mock(ParseHttpClient.class); - when(restClient.execute(any(ParseHttpRequest.class))).thenReturn(response); + when(restClient.execute(any(ParseHttpRequest.class))).thenReturn(mockResponse); File root = temporaryFolder.getRoot(); ParseFileController controller = new ParseFileController(restClient, root); @@ -290,13 +295,14 @@ public void testFetchAsyncCached() throws Exception { @Test public void testFetchAsyncSuccess() throws Exception { byte[] data = "hello".getBytes(); - ParseHttpResponse response = mock(ParseHttpResponse.class); - when(response.getStatusCode()).thenReturn(200); - when(response.getContent()).thenReturn(new ByteArrayInputStream(data)); - when(response.getTotalSize()).thenReturn((long) data.length); + ParseHttpResponse mockResponse = new ParseHttpResponse.Builder() + .setStatusCode(200) + .setTotalSize((long) data.length) + .setContent(new ByteArrayInputStream(data)) + .build(); ParseHttpClient awsClient = mock(ParseHttpClient.class); - when(awsClient.execute(any(ParseHttpRequest.class))).thenReturn(response); + when(awsClient.execute(any(ParseHttpRequest.class))).thenReturn(mockResponse); File root = temporaryFolder.getRoot(); ParseFileController controller = new ParseFileController(null, root).awsClient(awsClient); diff --git a/Parse/src/test/java/com/parse/ParseHttpClientTest.java b/Parse/src/test/java/com/parse/ParseHttpClientTest.java index a70b2ca7c..2be3568e1 100644 --- a/Parse/src/test/java/com/parse/ParseHttpClientTest.java +++ b/Parse/src/test/java/com/parse/ParseHttpClientTest.java @@ -8,6 +8,8 @@ */ package com.parse; +import com.parse.http.ParseHttpRequest; +import com.parse.http.ParseHttpResponse; import com.squareup.okhttp.Headers; import com.squareup.okhttp.mockwebserver.MockResponse; import com.squareup.okhttp.mockwebserver.MockWebServer; diff --git a/Parse/src/test/java/com/parse/ParseHttpRequestTest.java b/Parse/src/test/java/com/parse/ParseHttpRequestTest.java index 2e73f6171..6f1adae8e 100644 --- a/Parse/src/test/java/com/parse/ParseHttpRequestTest.java +++ b/Parse/src/test/java/com/parse/ParseHttpRequestTest.java @@ -8,9 +8,11 @@ */ package com.parse; +import com.parse.http.ParseHttpBody; +import com.parse.http.ParseHttpRequest; + import org.junit.Test; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map; diff --git a/Parse/src/test/java/com/parse/ParseHttpResponseTest.java b/Parse/src/test/java/com/parse/ParseHttpResponseTest.java index a7dd23888..6380d3d3b 100644 --- a/Parse/src/test/java/com/parse/ParseHttpResponseTest.java +++ b/Parse/src/test/java/com/parse/ParseHttpResponseTest.java @@ -8,6 +8,8 @@ */ package com.parse; +import com.parse.http.ParseHttpResponse; + import org.junit.Test; import java.io.ByteArrayInputStream; diff --git a/Parse/src/test/java/com/parse/ParseOkHttpClientTest.java b/Parse/src/test/java/com/parse/ParseOkHttpClientTest.java index 22efaa49a..a452b62fc 100644 --- a/Parse/src/test/java/com/parse/ParseOkHttpClientTest.java +++ b/Parse/src/test/java/com/parse/ParseOkHttpClientTest.java @@ -8,6 +8,9 @@ */ package com.parse; +import com.parse.http.ParseHttpRequest; +import com.parse.http.ParseHttpResponse; +import com.parse.http.ParseNetworkInterceptor; import com.squareup.okhttp.MediaType; import com.squareup.okhttp.Protocol; import com.squareup.okhttp.Request; diff --git a/Parse/src/test/java/com/parse/ParsePushControllerTest.java b/Parse/src/test/java/com/parse/ParsePushControllerTest.java index e7873e6e6..7223953da 100644 --- a/Parse/src/test/java/com/parse/ParsePushControllerTest.java +++ b/Parse/src/test/java/com/parse/ParsePushControllerTest.java @@ -8,6 +8,9 @@ */ package com.parse; +import com.parse.http.ParseHttpRequest; +import com.parse.http.ParseHttpResponse; + import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; diff --git a/Parse/src/test/java/com/parse/ParseRESTCommandTest.java b/Parse/src/test/java/com/parse/ParseRESTCommandTest.java index bef0f876c..f01406dcc 100644 --- a/Parse/src/test/java/com/parse/ParseRESTCommandTest.java +++ b/Parse/src/test/java/com/parse/ParseRESTCommandTest.java @@ -8,6 +8,9 @@ */ package com.parse; +import com.parse.http.ParseHttpRequest; +import com.parse.http.ParseHttpResponse; + import org.json.JSONArray; import org.json.JSONObject; import org.junit.After; @@ -51,11 +54,12 @@ private static ParseHttpResponse newMockParseHttpResponse(int statusCode, JSONOb } private static ParseHttpResponse newMockParseHttpResponse(int statusCode, String body) { - ParseHttpResponse response = mock(ParseHttpResponse.class); - when(response.getStatusCode()).thenReturn(statusCode); - when(response.getContent()).thenReturn(new ByteArrayInputStream(body.getBytes())); - when(response.getTotalSize()).thenReturn((long) body.length()); - return response; + ParseHttpResponse mockResponse = new ParseHttpResponse.Builder() + .setStatusCode(statusCode) + .setTotalSize((long) body.length()) + .setContent(new ByteArrayInputStream(body.getBytes())) + .build(); + return mockResponse; } @Rule @@ -468,13 +472,14 @@ public void testOnResponseCloseNetworkStreamWithNormalResponse() throws Exceptio .when(mockResponseStream) .close(); // Mock response - ParseHttpResponse response = mock(ParseHttpResponse.class); - when(response.getStatusCode()).thenReturn(statusCode); - when(response.getContent()).thenReturn(mockResponseStream); - when(response.getTotalSize()).thenReturn((long) bodyStr.length()); + ParseHttpResponse mockResponse = new ParseHttpResponse.Builder() + .setStatusCode(statusCode) + .setTotalSize((long) bodyStr.length()) + .setContent(mockResponseStream) + .build(); ParseRESTCommand command = new ParseRESTCommand.Builder().build(); - JSONObject json = ParseTaskUtils.wait(command.onResponseAsync(response, null)); + JSONObject json = ParseTaskUtils.wait(command.onResponseAsync(mockResponse, null)); verify(mockResponseStream, times(1)).close(); assertEquals(bodyJson, json, JSONCompareMode.NON_EXTENSIBLE); @@ -496,14 +501,15 @@ public void testOnResposneCloseNetworkStreamWithIOException() throws Exception { .when(mockResponseStream) .read(any(byte[].class)); // Mock response - ParseHttpResponse response = mock(ParseHttpResponse.class); - when(response.getStatusCode()).thenReturn(statusCode); - when(response.getContent()).thenReturn(mockResponseStream); + ParseHttpResponse mockResponse = new ParseHttpResponse.Builder() + .setStatusCode(statusCode) + .setContent(mockResponseStream) + .build(); ParseRESTCommand command = new ParseRESTCommand.Builder().build(); // We can not use ParseTaskUtils here since it will replace the original exception with runtime // exception - Task responseTask = command.onResponseAsync(response, null); + Task responseTask = command.onResponseAsync(mockResponse, null); responseTask.waitForCompletion(); assertTrue(responseTask.isFaulted()); diff --git a/Parse/src/test/java/com/parse/ParseRESTQueryCommandTest.java b/Parse/src/test/java/com/parse/ParseRESTQueryCommandTest.java index 0a831dafe..f2848a814 100644 --- a/Parse/src/test/java/com/parse/ParseRESTQueryCommandTest.java +++ b/Parse/src/test/java/com/parse/ParseRESTQueryCommandTest.java @@ -8,6 +8,8 @@ */ package com.parse; +import com.parse.http.ParseHttpRequest; + import org.json.JSONArray; import org.json.JSONObject; import org.junit.Test; diff --git a/Parse/src/test/java/com/parse/ParseRESTUserCommandTest.java b/Parse/src/test/java/com/parse/ParseRESTUserCommandTest.java index d21b3406c..7a4f4391c 100644 --- a/Parse/src/test/java/com/parse/ParseRESTUserCommandTest.java +++ b/Parse/src/test/java/com/parse/ParseRESTUserCommandTest.java @@ -8,6 +8,9 @@ */ package com.parse; +import com.parse.http.ParseHttpRequest; +import com.parse.http.ParseHttpResponse; + import org.json.JSONObject; import org.junit.After; import org.junit.Before; diff --git a/Parse/src/test/java/com/parse/ParseRequestTest.java b/Parse/src/test/java/com/parse/ParseRequestTest.java index 41ccaf446..09e72a3dc 100644 --- a/Parse/src/test/java/com/parse/ParseRequestTest.java +++ b/Parse/src/test/java/com/parse/ParseRequestTest.java @@ -8,6 +8,10 @@ */ package com.parse; +import com.parse.http.ParseHttpBody; +import com.parse.http.ParseHttpRequest; +import com.parse.http.ParseHttpResponse; + import org.junit.After; import org.junit.AfterClass; import org.junit.Before; @@ -76,10 +80,11 @@ public void testRetryLogic() throws Exception { // TODO(grantland): Move to ParseAWSRequestTest or ParseCountingByteArrayHttpBodyTest @Test public void testDownloadProgress() throws Exception { - ParseHttpResponse mockResponse = mock(ParseHttpResponse.class); - when(mockResponse.getStatusCode()).thenReturn(200); - when(mockResponse.getContent()).thenReturn(new ByteArrayInputStream(data)); - when(mockResponse.getTotalSize()).thenReturn((long) data.length); + ParseHttpResponse mockResponse = new ParseHttpResponse.Builder() + .setStatusCode(200) + .setTotalSize((long) data.length) + .setContent(new ByteArrayInputStream(data)) + .build(); ParseHttpClient mockHttpClient = mock(ParseHttpClient.class); when(mockHttpClient.execute(any(ParseHttpRequest.class))).thenReturn(mockResponse); diff --git a/Parse/src/test/java/com/parse/ParseTestUtils.java b/Parse/src/test/java/com/parse/ParseTestUtils.java index 19b2cf87a..13b29a342 100644 --- a/Parse/src/test/java/com/parse/ParseTestUtils.java +++ b/Parse/src/test/java/com/parse/ParseTestUtils.java @@ -8,6 +8,9 @@ */ package com.parse; +import com.parse.http.ParseHttpRequest; +import com.parse.http.ParseHttpResponse; + import org.json.JSONObject; import java.io.ByteArrayInputStream; diff --git a/Parse/src/test/java/com/parse/ParseURLConnectionHttpClientTest.java b/Parse/src/test/java/com/parse/ParseURLConnectionHttpClientTest.java index 5dec9c59e..d35959a19 100644 --- a/Parse/src/test/java/com/parse/ParseURLConnectionHttpClientTest.java +++ b/Parse/src/test/java/com/parse/ParseURLConnectionHttpClientTest.java @@ -8,6 +8,9 @@ */ package com.parse; +import com.parse.http.ParseHttpRequest; +import com.parse.http.ParseHttpResponse; + import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; @@ -23,7 +26,6 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; -import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; // For android.net.SSLCertificateSocketFactory