Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<dependency>
<groupId>smartrics.restfixture</groupId>
<artifactId>smartrics-RestClient</artifactId>
<version>2.2</version>
<version>2.3-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.mozilla</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ public void PUT() {
restFixture.PUT();
}

/**
* delegates to {@link RestFixture#PATCH()}
*/
public void PATCH() {
restFixture.PATCH();
}
/**
* delegates to {@link RestFixture#GET()}
*/
Expand Down
24 changes: 11 additions & 13 deletions src/main/java/smartrics/rest/fitnesse/fixture/PartsFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@
*/
package smartrics.rest.fitnesse.fixture;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpURL;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.URIException;

import org.apache.http.client.HttpClient;
import smartrics.rest.client.RestClient;
import smartrics.rest.client.RestClientImpl;
import smartrics.rest.client.RestRequest;
Expand All @@ -36,6 +32,10 @@
import smartrics.rest.fitnesse.fixture.support.ContentType;
import smartrics.rest.fitnesse.fixture.support.HttpClientBuilder;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;

/**
* Factory of all dependencies the rest fixture needs.
*
Expand All @@ -55,20 +55,18 @@ public PartsFactory(final RunnerVariablesProvider variablesProvider, Config conf
*
* @param config
* the configuration for the rest client to build
* @param followRedirects
* @return the rest client
*/
public RestClient buildRestClient(final Config config) {
HttpClient httpClient = new HttpClientBuilder().createHttpClient(config);
public RestClient buildRestClient(final Config config, boolean followRedirects) {
HttpClient httpClient = new HttpClientBuilder().createHttpClient(config, followRedirects);
return new RestClientImpl(httpClient) {

@Override
protected URI createUri(String uriString, boolean escaped) throws URIException {
boolean useNewHttpUriFactory = config.getAsBoolean("http.client.use.new.http.uri.factory", false);
if (useNewHttpUriFactory) {
return new HttpURL(uriString);
}
protected URI createUri(String uriString, boolean escaped) throws URISyntaxException, UnsupportedEncodingException {
return super.createUri(uriString, escaped);
}

@Override
public String getMethodClassnameFromMethodName(String mName) {
boolean useOverriddenHttpMethodImpl = config.getAsBoolean("http.client.use.new.http.uri.factory", false);
Expand Down
27 changes: 23 additions & 4 deletions src/main/java/smartrics/rest/fitnesse/fixture/RestFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ public Variables createRunnerVariables() {
case SLIM:
return new SlimVariables(config, slimStatementExecutor);
case FIT:
return new FitVariables(config);
default:
// Use FitVariables for tests
return new FitVariables(config);
Expand Down Expand Up @@ -710,6 +709,26 @@ public void PUT() {
debugMethodCallEnd();
}

/**
* <code> | PATCH | URL | ?ret | ?headers | ?body |</code>
* <p>
* executes a PATCH on the URL and checks the return (a string representation
* the operation return code), the HTTP response headers and the HTTP
* response body
* <p>
* URL is resolved by replacing global variables previously defined with
* <code>let()</code>
* <p>
* the HTTP request headers can be set via <code>setHeaders()</code>. If not
* set, the list of default headers will be set. See
* <code>DEF_REQUEST_HEADERS</code>
*/
public void PATCH() {
debugMethodCallStart();
doMethod(emptifyBody(requestBody), "Patch");
debugMethodCallEnd();
}

/**
* <code> | GET | uri | ?ret | ?headers | ?body |</code>
* <p>
Expand Down Expand Up @@ -1312,10 +1331,10 @@ private void configFixture() {
/**
* Allows to config the rest client implementation. the method shoudl
* configure the instance attribute {@link RestFixture#restClient} created
* by the {@link smartrics.rest.fitnesse.fixture.PartsFactory#buildRestClient(smartrics.rest.fitnesse.fixture.support.Config)}.
* by the {@link PartsFactory#buildRestClient(Config, boolean)}.
*/
private void configRestClient() {
restClient = partsFactory.buildRestClient(getConfig());
restClient = partsFactory.buildRestClient(getConfig(), followRedirects);
}

@SuppressWarnings({ "rawtypes", "unchecked" })
Expand Down Expand Up @@ -1371,7 +1390,7 @@ private void configureCredentials() {
Config newConfig = getConfig();
newConfig.add("http.basicauth.username", newUsername);
newConfig.add("http.basicauth.password", newPassword);
restClient = partsFactory.buildRestClient(newConfig);
restClient = partsFactory.buildRestClient(newConfig, followRedirects);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,23 @@
*/
package smartrics.rest.fitnesse.fixture.support;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.params.HostParams;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.DefaultRedirectStrategy;

import java.util.concurrent.TimeUnit;


/**
* Helper builder class for an apache {@link HttpClient} that uses data in the
* {@link Config} to configure the object.
*
*
* @author smartrics
*
*
*/
public class HttpClientBuilder {
/**
Expand All @@ -48,50 +50,36 @@ public class HttpClientBuilder {

/**
* @param config the {@link Config} containing the client configuration paramteres.
* @param followRedirects
* @return an instance of an {@link HttpClient}.
*/
public HttpClient createHttpClient(final Config config) {
HttpClient client = createConfiguredClient(config);
if (config != null) {
configureHost(config, client);
configureCredentials(config, client);
}
return client;
}

private HttpClient createConfiguredClient(final Config config) {
HttpClientParams params = new HttpClientParams();
params.setSoTimeout(DEFAULT_SO_TO);
if (config != null) {
params.setSoTimeout(config.getAsInteger("http.client.connection.timeout", DEFAULT_SO_TO));
public HttpClient createHttpClient(final Config config, boolean followRedirects) {
int timeout = config == null ? DEFAULT_SO_TO : config.getAsInteger("http.client.connection.timeout", DEFAULT_SO_TO);
org.apache.http.impl.client.HttpClientBuilder builder = org.apache.http.impl.client.HttpClientBuilder.create()
.setConnectionTimeToLive(timeout, TimeUnit.MILLISECONDS);
if (followRedirects) {
builder.setRedirectStrategy(new DefaultRedirectStrategy());
} else {
builder.disableRedirectHandling();
}
HttpClient client = new HttpClient(params);
return client;
}

private void configureHost(final Config config, HttpClient client) {
HostConfiguration hostConfiguration = client.getHostConfiguration();
String proxyHost = config.get("http.proxy.host");
if (proxyHost != null) {
int proxyPort = config.getAsInteger("http.proxy.port", DEFAULT_PROXY_PORT);
hostConfiguration.setProxy(proxyHost, proxyPort);
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
builder.setProxy(proxy);
configureProxyCredentials(config, builder, proxyHost, proxyPort);
}
HostParams hostParams = new HostParams();
hostConfiguration.setParams(hostParams);
return builder.build();
}

private void configureCredentials(final Config config, HttpClient client) {
private void configureProxyCredentials(Config config, org.apache.http.impl.client.HttpClientBuilder builder, String proxyHost, int proxyPort) {
String username = config.get("http.basicauth.username");
String password = config.get("http.basicauth.password");
if (username != null && password != null) {
Credentials defaultcreds = new UsernamePasswordCredentials(username, password);
client.getState().setCredentials(AuthScope.ANY, defaultcreds);
}
String proxyUsername = config.get("http.proxy.username");
String proxyPassword = config.get("http.proxy.password");
if (proxyUsername != null && proxyPassword != null) {
Credentials defaultcreds = new UsernamePasswordCredentials(proxyUsername, proxyPassword);
client.getState().setProxyCredentials(AuthScope.ANY, defaultcreds);
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials(username, password));
builder.setDefaultCredentialsProvider(credentialsProvider);
}
}

}

This file was deleted.

This file was deleted.

This file was deleted.

Loading