Skip to content

Instantly share code, notes, and snippets.

@badalsarkar

badalsarkar/MockNetworkTest.java Secret

Last active Nov 29, 2020
Embed
What would you like to do?
Blog-2020-11-27-testing-and-ci-workflow-blink
# This workflow will build a Java project with Maven
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
name: Java CI with Maven
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.11
uses: actions/setup-java@v1
with:
java-version: 1.11
- name: Cache Maven packages
uses: actions/cache@v2
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Build with Maven
run: mvn -B verify --file pom.xml
public class TestChecker {
private static HttpApiSimulation mockServer = null;
@BeforeAll
public static void setUp() {
mockServer = httpApiSimulation("mockSim");
// defining mock response
mockServer.add(
simlet("test-google-good")
.when(
httpRequest()
.whereMethod("HEAD")
.whereUriPath(isEqualTo("/return/200"))
.whereHeader("Host", contains("google.com")))
.then(httpResponse().withStatus(200).withHeader("Content-Type", "application/text")));
mockServer.add(
simlet("test-google-bad")
.when(
httpRequest()
.whereMethod("HEAD")
.whereUriPath(isEqualTo("/return/404"))
.whereHeader("Host", contains("google.com")))
.then(httpResponse().withStatus(404).withHeader("Content-Type", "application/text")));
mockServer.add(
simlet("test-google-redirect")
.when(
httpRequest()
.whereMethod("HEAD")
.whereUriPath(isEqualTo("/return/301"))
.whereHeader("Host", contains("google.com")))
.then(httpResponse().withStatus(301).withHeader("Content-Type", "application/text")));
}
@AfterAll
public static void tearDown() {
if (mockServer != null) {
mockServer.stop();
}
}
@Test
public void testGoodUrl() throws Exception {
try {
System.setProperty("http.proxyHost", "localhost");
System.setProperty("http.proxyPort", "6090");
UrlStatus url = Checker.makeRequest("http://www.google.com/return/200");
Assertions.assertEquals(200, url.getStatusCode());
} finally {
System.clearProperty("http.proxyHost");
System.clearProperty("http.proxyPort");
}
}
@Test
public void testBadUrl() throws Exception {
try {
System.setProperty("http.proxyHost", "localhost");
System.setProperty("http.proxyPort", "6090");
UrlStatus url = Checker.makeRequest("http://www.google.com/return/404");
Assertions.assertEquals(404, url.getStatusCode());
} finally {
System.clearProperty("http.proxyHost");
System.clearProperty("http.proxyPort");
}
}
@Test
public void testRedirectUrl() throws Exception {
try {
System.setProperty("http.proxyHost", "localhost");
System.setProperty("http.proxyPort", "6090");
UrlStatus url = Checker.makeRequest("http://www.google.com/return/301");
Assertions.assertEquals(301, url.getStatusCode());
} finally {
System.clearProperty("http.proxyHost");
System.clearProperty("http.proxyPort");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment