Skip to content

Commit 5aba37b

Browse files
committed
fix multiple servers test with ProdWebServerRule
1 parent 6c8d07c commit 5aba37b

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

src/test/java/net/codestory/http/testhelpers/ProdWebServerRule.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@
1515
*/
1616
package net.codestory.http.testhelpers;
1717

18-
import static net.codestory.http.Configuration.NO_ROUTE;
19-
import static net.codestory.http.misc.MemoizingSupplier.*;
20-
21-
import java.util.function.*;
18+
import net.codestory.http.Configuration;
19+
import net.codestory.http.WebServer;
20+
import net.codestory.http.misc.Env;
21+
import org.junit.rules.ExternalResource;
2222

23-
import net.codestory.http.*;
24-
import net.codestory.http.misc.*;
23+
import java.util.function.Supplier;
2524

26-
import org.junit.rules.*;
25+
import static net.codestory.http.Configuration.NO_ROUTE;
26+
import static net.codestory.http.misc.MemoizingSupplier.memoize;
2727

2828
public class ProdWebServerRule extends ExternalResource {
29-
private static Supplier<WebServer> server = memoize(() -> new WebServer() {
29+
private final Supplier<WebServer> server = memoize(() -> new WebServer() {
3030
@Override
3131
protected Env createEnv() {
3232
return Env.prod();
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright (C) 2013-2015 all@code-story.net
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 net.codestory.http.testhelpers;
17+
18+
19+
import okhttp3.OkHttpClient;
20+
import okhttp3.Request;
21+
import okhttp3.Response;
22+
import org.junit.ClassRule;
23+
import org.junit.Test;
24+
25+
import java.io.IOException;
26+
27+
import static java.lang.String.format;
28+
import static org.assertj.core.api.Java6Assertions.assertThat;
29+
30+
public class ProdWebServerRuleTest {
31+
@ClassRule public static ProdWebServerRule server1 = new ProdWebServerRule();
32+
@ClassRule public static ProdWebServerRule server2 = new ProdWebServerRule();
33+
OkHttpClient client = new OkHttpClient();
34+
35+
@Test
36+
public void test_2_servers_rules_should_be_independent() throws IOException {
37+
server1.configure(routes -> routes.get("/index", "hello world 1"));
38+
server2.configure(routes -> routes.get("/index", "hello world 2"));
39+
try (Response response = client.newCall(new Request.Builder().url(format("http://localhost:%s/index", server1.port())).build()).execute()) {
40+
assertThat(response.body().string()).isEqualTo("hello world 1");
41+
}
42+
try (Response response = client.newCall(new Request.Builder().url(format("http://localhost:%s/index", server2.port())).build()).execute()) {
43+
assertThat(response.body().string()).isEqualTo("hello world 2");
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)