Skip to content

Commit

Permalink
[#17] Fix UsingWithHttpClientTest to pass in TravisCI.
Browse files Browse the repository at this point in the history
  • Loading branch information
dzieciou committed Jan 2, 2019
1 parent 52131bb commit ff4d639
Showing 1 changed file with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,42 +1,55 @@
package com.github.dzieciou.testing.curl;


import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.MatcherAssert.assertThat;

import com.github.valfirst.slf4jtest.LoggingEvent;
import com.github.valfirst.slf4jtest.TestLoggerFactory;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.testng.annotations.Test;

public class UsingWithHttpClientTest {

private static HttpClient createHttpClient() {
return HttpClientBuilder.create()
.addInterceptorFirst(new CurlLoggingInterceptor(Options.builder()
.targetPlatform(Platform.UNIX) // TO ease verifying output curl
.build()))
.build();
}

@Test(groups = "end-to-end-samples")
public void testHttp() throws IOException {
TestLoggerFactory.clearAll();
HttpGet getRequest = new HttpGet("http://google.com");
createHttpClient().execute(getRequest);
assertThat(TestLoggerFactory.getAllLoggingEvents().get(0).getMessage(),
equalTo("curl 'http://google.com/' --compressed --insecure --verbose"));
assertThat(getAllLoggedMessages(),
hasItem("curl 'http://google.com/' --compressed --insecure --verbose"));
}



@Test(groups = "end-to-end-samples")
public void testHttps() throws IOException {
TestLoggerFactory.clearAll();
HttpGet getRequest = new HttpGet("https://google.com");
createHttpClient().execute(getRequest);
assertThat(TestLoggerFactory.getAllLoggingEvents().get(0).getMessage(),
equalTo("curl 'https://google.com/' --compressed --insecure --verbose"));
assertThat(getAllLoggedMessages(),
hasItem("curl 'https://google.com/' --compressed --insecure --verbose"));
}


private static HttpClient createHttpClient() {
return HttpClientBuilder.create()
.addInterceptorFirst(new CurlLoggingInterceptor(Options.builder()
.targetPlatform(Platform.UNIX) // TO ease verifying output curl
.build()))
.build();
}

private static List<String> getAllLoggedMessages() {
return TestLoggerFactory.getAllLoggingEvents().stream().map(LoggingEvent::getMessage).collect(
Collectors.toList());
}


}

0 comments on commit ff4d639

Please sign in to comment.