Created
October 16, 2016 20:36
-
-
Save dherges/5785f9aeb7a0e96e73d8f4ea8861a757 to your computer and use it in GitHub Desktop.
spring-cucumber
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.boot.context.embedded.LocalServerPort; | |
import java.io.IOException; | |
import cucumber.api.java8.En; | |
import okhttp3.OkHttpClient; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
import static org.assertj.core.api.Assertions.assertThat; | |
import static org.assertj.core.api.Assertions.fail; | |
public class ClientSteps implements En { | |
@LocalServerPort // <-- injects the tcp port of the embedded web server | |
private int serverPort; | |
private Response response; | |
public ClientSteps() { | |
When("client calls ([A-Z]{0,4}) (.*)", (String verb, String path) -> { | |
try { | |
response = new OkHttpClient.Builder().build() | |
.newCall( | |
new Request.Builder() | |
.method(verb, null) | |
.url("http://localhost:" + serverPort + path) | |
.build() | |
) | |
.execute(); | |
} catch (IOException e) { | |
fail("Cannot execute HTTP request", e); | |
} | |
}); | |
Then("response code is ([0-9]+)", (Integer code) -> { | |
assertThat(response.code()).isEqualTo(code); | |
}); | |
Then("response body is", (String docString) -> { | |
assertThat(response.body().string()).isEqualToIgnoringCase(docString); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment