com.lyncode.test.http.HttpClient Maven / Gradle / Ivy
/**
* 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.lyncode.test.http;
import com.lyncode.test.http.method.MethodBuilder;
import org.apache.http.HttpResponse;
import org.apache.http.impl.client.HttpClients;
import org.hamcrest.Matcher;
import org.hamcrest.core.IsCollectionContaining;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class HttpClient {
private final org.apache.http.client.HttpClient client = HttpClients.createDefault();
private final String baseUrl;
private final List responses = new ArrayList();
public HttpClient(String baseUrl) {
this.baseUrl = baseUrl;
}
public HttpResponse receives(MethodBuilder methodBuilder) throws IOException {
methodBuilder.setBaseUrl(baseUrl);
HttpResponse response = client.execute(methodBuilder.build());
responses.add(response);
return response;
}
public HttpResponse sends () {
if (responses.isEmpty()) return null;
else return responses.get(responses.size() - 1);
}
public boolean sent (Matcher responseMatcher) {
return IsCollectionContaining.hasItem(responseMatcher).matches(responses);
}
}