{"id":1436,"date":"2023-07-05T12:17:50","date_gmt":"2023-07-05T11:17:50","guid":{"rendered":"https:\/\/www.thomaskeller.biz\/blog\/?p=1436"},"modified":"2023-07-05T12:17:50","modified_gmt":"2023-07-05T11:17:50","slug":"testing-https-requests-with-wiremock-and-robolectric","status":"publish","type":"post","link":"https:\/\/www.thomaskeller.biz\/blog\/2023\/07\/05\/testing-https-requests-with-wiremock-and-robolectric\/","title":{"rendered":"Testing HTTPS Requests with Wiremock and Robolectric"},"content":{"rendered":"\n<p>Prerequisites: OkHttp 3.x\/4.x, Wiremock 2.x, Robolectric 4.9.x<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ build.gradle.kts\ndependencies {\n  testImplementation(\"org.robolectric:robolectric:4.9.2\"\n  testImplementation(\"com.github.tomakehurst:wiremock-jre8-standalone:2.35.0\")\n  testImplementation(\"com.squareup.okhttp3:okhttp-tls:4.10.0\")\n}<\/code><\/pre>\n\n\n\n<p>Now for the actual test it&#8217;s important that your System Under Test (SUT) is able to configure it&#8217;s <code>OkHttpClient<\/code> instance:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class SomeHttpClient @VisibleForTesting internal constructor(client: OkHttpClient) {\n\n  constructor() : this(OkHttpClient())\n\n  fun execute(request: Request): Response =\n    client.newCall(request).execute()\n}<\/code><\/pre>\n\n\n\n<p>&#8230;so that in your test you can configure the needed SSL building blocks:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>...\nval handshakeCertificates = HandshakeCertificates.Builder()\n  .addInsecureHost(\"localhost\")\n  .build()\nval hostnameVerifier = HostnameVerifier { hostname, _ -> hostname == \"localhost\" }\n\nval sut = SomeHttpClient(\n  OkHttpClient.Builder()\n    .sslSocketFactory(handshakeCertificates.sslSocketFactory(), handshakeCertificates.trustManager)\n    .hostnameVerifier(hostnameVerifier)\n    .build()\n)\n...<\/code><\/pre>\n\n\n\n<p>For the Robolectric setup, you need to ensure that you disable Robolectric&#8217;s conscrypt implementation, because this does not work well with Wiremock:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@RunWith(RobolectricTestRunner::class)\n@ConscryptMode(ConscryptMode.Mode.OFF)\nclass SomeHttpClientTest {\n  @get:Rule\n  val wiremockRule = WireMockRule(wireMockConfig().dynamicHttpsPort())\n\n  \/\/ val sut = ... (see above)\n\n  @Test\n  fun shouldRequestSomething() {\n    stubFor(\n      get(urlPathMatching(\"\/echo$\"))\n        .willReturn(aResponse().withBody(\"Hello World!\"))\n      )\n    )\n    \n    val response = sut.execute(\n      Request.Builder()\n        .url(\"${wiremockRule.baseUrl()}\/echo\")\n        .get()\n        .build()\n      )\n\n    \/\/ verify response\n  }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Prerequisites: OkHttp 3.x\/4.x, Wiremock 2.x, Robolectric 4.9.x Now for the actual test it&#8217;s important that your System Under Test (SUT) is able to configure it&#8217;s OkHttpClient instance: &#8230;so that in your test you can configure the needed SSL building blocks: For the Robolectric setup, you need to ensure that you disable Robolectric&#8217;s conscrypt implementation, because &hellip; <a href=\"https:\/\/www.thomaskeller.biz\/blog\/2023\/07\/05\/testing-https-requests-with-wiremock-and-robolectric\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Testing HTTPS Requests with Wiremock and Robolectric<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1436","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/posts\/1436","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/comments?post=1436"}],"version-history":[{"count":1,"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/posts\/1436\/revisions"}],"predecessor-version":[{"id":1437,"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/posts\/1436\/revisions\/1437"}],"wp:attachment":[{"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/media?parent=1436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/categories?post=1436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/tags?post=1436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}