forked from Azure/azure-sdk-for-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ProtocolPolicy and tests (Azure#305)
- Loading branch information
Dan Schulte
authored
Nov 28, 2017
1 parent
efa0049
commit bb0fe5b
Showing
2 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
client-runtime/src/main/java/com/microsoft/rest/v2/policy/ProtocolPolicy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for | ||
* license information. | ||
*/ | ||
|
||
package com.microsoft.rest.v2.policy; | ||
|
||
|
||
import com.microsoft.rest.v2.http.HttpRequest; | ||
import com.microsoft.rest.v2.http.HttpResponse; | ||
import com.microsoft.rest.v2.http.UrlBuilder; | ||
import rx.Single; | ||
|
||
/** | ||
* A RequestPolicy that adds the provided protocol/scheme to each HttpRequest. | ||
*/ | ||
public class ProtocolPolicy implements RequestPolicy { | ||
private final RequestPolicy nextPolicy; | ||
private final String protocol; | ||
private final boolean overwrite; | ||
|
||
ProtocolPolicy(RequestPolicy nextPolicy, String protocol, boolean overwrite) { | ||
this.nextPolicy = nextPolicy; | ||
this.protocol = protocol; | ||
this.overwrite = overwrite; | ||
} | ||
|
||
@Override | ||
public Single<HttpResponse> sendAsync(HttpRequest request) { | ||
final UrlBuilder urlBuilder = UrlBuilder.parse(request.url()); | ||
if (overwrite || urlBuilder.scheme() == null) { | ||
request.withUrl(urlBuilder.withScheme(protocol).toString()); | ||
} | ||
return nextPolicy.sendAsync(request); | ||
} | ||
|
||
/** | ||
* A RequestPolicy.Factory class that creates ProtocolPolicy objects. | ||
*/ | ||
public static class Factory implements RequestPolicy.Factory { | ||
private final String protocol; | ||
private final boolean overwrite; | ||
|
||
/** | ||
* Create a new ProtocolPolicy.Factory object. | ||
* @param protocol The protocol to set on every HttpRequest. | ||
*/ | ||
public Factory(String protocol) { | ||
this(protocol, true); | ||
} | ||
|
||
/** | ||
* Create a new ProtocolPolicy.Factory object. | ||
* @param protocol The protocol to set. | ||
* @param overwrite Whether or not to overwrite a HttpRequest's protocol if it already has one. | ||
*/ | ||
public Factory(String protocol, boolean overwrite) { | ||
this.protocol = protocol; | ||
this.overwrite = overwrite; | ||
} | ||
|
||
@Override | ||
public ProtocolPolicy create(RequestPolicy next, Options options) { | ||
return new ProtocolPolicy(next, protocol, overwrite); | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
client-runtime/src/test/java/com/microsoft/rest/v2/policy/ProtocolPolicyTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.microsoft.rest.v2.policy; | ||
|
||
import com.microsoft.rest.v2.http.HttpRequest; | ||
import com.microsoft.rest.v2.http.HttpResponse; | ||
import org.junit.Test; | ||
import rx.Single; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class ProtocolPolicyTests { | ||
|
||
@Test | ||
public void withOverwriteAndNoProtocol() { | ||
final ProtocolPolicy policy = createProtocolPolicy("ftp", "ftp://www.bing.com"); | ||
policy.sendAsync(createHttpRequest("www.bing.com")); | ||
} | ||
|
||
@Test | ||
public void withOverwriteAndProtocol() { | ||
final ProtocolPolicy policy = createProtocolPolicy("ftp", "ftp://www.bing.com"); | ||
policy.sendAsync(createHttpRequest("http://www.bing.com")); | ||
} | ||
|
||
@Test | ||
public void withNoOverwriteAndNoProtocol() { | ||
final ProtocolPolicy policy = createProtocolPolicy("ftp", false, "ftp://www.bing.com"); | ||
policy.sendAsync(createHttpRequest("www.bing.com")); | ||
} | ||
|
||
@Test | ||
public void withNoOverwriteAndProtocol() { | ||
final ProtocolPolicy policy = createProtocolPolicy("ftp", false, "https://www.bing.com"); | ||
policy.sendAsync(createHttpRequest("https://www.bing.com")); | ||
} | ||
|
||
private static RequestPolicy createMockRequestPolicy(final String expectedUrl) { | ||
return new RequestPolicy() { | ||
@Override | ||
public Single<HttpResponse> sendAsync(HttpRequest request) { | ||
assertEquals(expectedUrl, request.url()); | ||
return null; | ||
} | ||
}; | ||
} | ||
|
||
private static ProtocolPolicy createProtocolPolicy(String protocol, String expectedUrl) { | ||
return new ProtocolPolicy.Factory(protocol).create(createMockRequestPolicy(expectedUrl), null); | ||
} | ||
|
||
private static ProtocolPolicy createProtocolPolicy(String protocol, boolean overwrite, String expectedUrl) { | ||
return new ProtocolPolicy.Factory(protocol, overwrite).create(createMockRequestPolicy(expectedUrl), null); | ||
} | ||
|
||
private static HttpRequest createHttpRequest(String url) { | ||
return new HttpRequest("mock.caller", "GET", url); | ||
} | ||
} |