Skip to content

Commit

Permalink
Add ProtocolPolicy and tests (Azure#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Schulte authored Nov 28, 2017
1 parent efa0049 commit bb0fe5b
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
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);
}
}
}
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);
}
}

0 comments on commit bb0fe5b

Please sign in to comment.