|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 WireMock Inc, Oleg Nenashev and all project contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.wiremock.integrations.testcontainers; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.testcontainers.junit.jupiter.Container; |
| 20 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 21 | +import org.wiremock.integrations.testcontainers.testsupport.http.HttpResponse; |
| 22 | +import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpClient; |
| 23 | + |
| 24 | +import java.io.File; |
| 25 | + |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
| 27 | + |
| 28 | +@Testcontainers(parallel = true) |
| 29 | +class WireMockContainerRootDirTest { |
| 30 | + @Container |
| 31 | + WireMockContainer wiremockServer = new WireMockContainer(TestConfig.WIREMOCK_DEFAULT_IMAGE) |
| 32 | + .withMapping("hello", WireMockContainerRootDirTest.class, "hello.json") |
| 33 | + .withFileFromResource("file.json", WireMockContainerRootDirTest.class, "file.json") |
| 34 | + .withRootDir(new File("src/test/resources/root-dir")); |
| 35 | + |
| 36 | + @Test |
| 37 | + void testThatLoadMappingFromRootDirectory() throws Exception { |
| 38 | + // given |
| 39 | + String url = wiremockServer.getUrl("hello/root/dir"); |
| 40 | + |
| 41 | + // when |
| 42 | + HttpResponse response = new TestHttpClient().get(url); |
| 43 | + |
| 44 | + // then |
| 45 | + assertThat(response.getBody()) |
| 46 | + .as("response body") |
| 47 | + .contains("Hello root dir"); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void testThatLoadMappingFromNestedRootDirectory() throws Exception { |
| 52 | + // given |
| 53 | + String url = wiremockServer.getUrl("hello/nested/root/dir"); |
| 54 | + |
| 55 | + // when |
| 56 | + HttpResponse response = new TestHttpClient().get(url); |
| 57 | + |
| 58 | + // then |
| 59 | + assertThat(response.getBody()) |
| 60 | + .as("response body") |
| 61 | + .contains("Hello nested root dir"); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void testThatServesFileFromRootDirectory() throws Exception { |
| 66 | + // given |
| 67 | + String url = wiremockServer.getUrl("root-dir-file.json"); |
| 68 | + |
| 69 | + // when |
| 70 | + HttpResponse response = new TestHttpClient().get(url); |
| 71 | + |
| 72 | + // then |
| 73 | + assertThat(response.getBody()) |
| 74 | + .as("response body") |
| 75 | + .isEqualTo("{ \"message\": \"file from root dir\" }"); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void testThatServesNestedFileFromRootDirectory() throws Exception { |
| 80 | + // given |
| 81 | + String url = wiremockServer.getUrl("nested/root-dir-nested-file.json"); |
| 82 | + |
| 83 | + // when |
| 84 | + HttpResponse response = new TestHttpClient().get(url); |
| 85 | + |
| 86 | + // then |
| 87 | + assertThat(response.getBody()) |
| 88 | + .as("response body") |
| 89 | + .isEqualTo("{ \"message\": \"nested file from root dir\" }"); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void testThatDirectMappingsAndFilesAreLoadedWithCustomRootDirEnabled() throws Exception { |
| 94 | + // given |
| 95 | + String url = wiremockServer.getUrl("/hello"); |
| 96 | + |
| 97 | + // when |
| 98 | + HttpResponse response = new TestHttpClient().get(url); |
| 99 | + |
| 100 | + // then |
| 101 | + assertThat(response.getBody()) |
| 102 | + .as("Wrong response body") |
| 103 | + .contains("file contents from direct mapping"); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void testThatDirectMappingsAndFilesAreLoadedEvenWhenRootDirIsSpecified() throws Exception { |
| 108 | + // given |
| 109 | + String url = wiremockServer.getUrl("/hello"); |
| 110 | + |
| 111 | + // when |
| 112 | + HttpResponse response = new TestHttpClient().get(url); |
| 113 | + |
| 114 | + // then |
| 115 | + assertThat(response.getBody()) |
| 116 | + .as("Wrong response body") |
| 117 | + .contains("file contents from direct mapping"); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void testThatMappingsAndFileAreLoadedFromDefaultRootDir() throws Exception { |
| 122 | + |
| 123 | + try (WireMockContainer wmc = new WireMockContainer(TestConfig.WIREMOCK_DEFAULT_IMAGE)) { |
| 124 | + wmc.start(); |
| 125 | + |
| 126 | + // given |
| 127 | + String url = wmc.getUrl("/hello/default/root/dir"); |
| 128 | + |
| 129 | + // when |
| 130 | + HttpResponse response = new TestHttpClient().get(url); |
| 131 | + |
| 132 | + // then |
| 133 | + assertThat(response.getBody()) |
| 134 | + .as("Wrong response body") |
| 135 | + .contains("contents from default root dir file"); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + void testThatInvalidRootDirIsIgnored() throws Exception { |
| 141 | + |
| 142 | + try (WireMockContainer wmc = new WireMockContainer(TestConfig.WIREMOCK_DEFAULT_IMAGE) |
| 143 | + .withMapping("hello", WireMockContainerRootDirTest.class, "hello.json") |
| 144 | + .withFileFromResource("file.json", WireMockContainerRootDirTest.class, "file.json") |
| 145 | + .withRootDir(new File("invalid/root/dir"))) { |
| 146 | + wmc.start(); |
| 147 | + |
| 148 | + // given |
| 149 | + String url = wmc.getUrl("/hello"); |
| 150 | + |
| 151 | + // when |
| 152 | + HttpResponse response = new TestHttpClient().get(url); |
| 153 | + |
| 154 | + // then |
| 155 | + assertThat(response.getBody()) |
| 156 | + .as("Wrong response body") |
| 157 | + .contains("file contents from direct mapping"); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + void testThatNullRootDirIsIgnored() throws Exception { |
| 163 | + |
| 164 | + try (WireMockContainer wmc = new WireMockContainer(TestConfig.WIREMOCK_DEFAULT_IMAGE) |
| 165 | + .withMapping("hello", WireMockContainerRootDirTest.class, "hello.json") |
| 166 | + .withFileFromResource("file.json", WireMockContainerRootDirTest.class, "file.json") |
| 167 | + .withRootDir(null)) { |
| 168 | + wmc.start(); |
| 169 | + |
| 170 | + // given |
| 171 | + String url = wmc.getUrl("/hello"); |
| 172 | + |
| 173 | + // when |
| 174 | + HttpResponse response = new TestHttpClient().get(url); |
| 175 | + |
| 176 | + // then |
| 177 | + assertThat(response.getBody()) |
| 178 | + .as("Wrong response body") |
| 179 | + .contains("file contents from direct mapping"); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | +} |
0 commit comments