com.github.yongchristophertang.engine.web.WebTemplate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rest-connector Show documentation
Show all versions of rest-connector Show documentation
A test client framework for RESTful HTTP and Java APIs
/*
* Copyright 2014-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.yongchristophertang.engine.web;
import com.github.yongchristophertang.engine.AssertUtils;
import com.github.yongchristophertang.engine.web.request.RequestBuilder;
import com.github.yongchristophertang.engine.web.response.DefaultHttpResult;
import com.github.yongchristophertang.engine.web.response.DefaultResultActions;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.HttpClients;
import java.util.ArrayList;
import java.util.List;
/**
* Main entry point for Http engine support.
*
* Below is an example:
*
*
*
* WebTemplate webTemplate = WebTemplateBuilder.defaultConfig().build();
*
* webTemplate.perform(get(http://localhost:8080).param("test", "test")).andDo(print())
* .andExpect(content().contentType("application/json;charset=utf-8"))
* .andExpect(jsonPath("$.code", is("10004040"))).andTransform(json().parse("$.code")).andDo(print());
*
*
*
* @author Yong Tang
* @since 0.4
*/
public final class WebTemplate {
private HttpClient httpClient;
private List defaultResultMatchers = new ArrayList<>();
private List defaultResultHandlers = new ArrayList<>();
/**
* Access via {@link WebTemplateBuilder#build}
*/
WebTemplate(RequestConfig config) {
httpClient = HttpClients.custom().setDefaultRequestConfig(config).build();
}
public ResultActions perform(RequestBuilder builder) throws Exception {
HttpUriRequest httpRequest = (HttpUriRequest) builder.buildRequest();
long before = System.currentTimeMillis();
HttpResponse httpResponse = httpClient.execute(httpRequest);
long after = System.currentTimeMillis();
HttpResult httpResult = new DefaultHttpResult(httpRequest, httpResponse, after - before, builder.getRequestDescription());
applyDefaultResultMatchersAndHandlers(httpResult);
return new DefaultResultActions(httpResult);
}
private void applyDefaultResultMatchersAndHandlers(HttpResult httpResult) throws Exception {
for (ResultMatcher matcher : defaultResultMatchers) {
matcher.match(httpResult);
}
for (ResultHandler handler : defaultResultHandlers) {
handler.handle(httpResult);
}
}
/**
* Expectations to assert after every performed request.
*
* @see WebTemplateBuilder#alwaysExpect
*/
void setDefaultResultMatchers(List resultMatchers) {
AssertUtils.notNull(resultMatchers, "resultMatchers is required");
this.defaultResultMatchers = resultMatchers;
}
/**
* General actions to apply after every performed request.
*
* @see WebTemplateBuilder#alwaysDo
*/
void setDefaultResultHandlers(List resultHandlers) {
AssertUtils.notNull(resultHandlers, "resultHandlers is required");
this.defaultResultHandlers = resultHandlers;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy