-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTHttpDTest.java
More file actions
78 lines (66 loc) · 2.68 KB
/
THttpDTest.java
File metadata and controls
78 lines (66 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package org.sfj;
import org.apache.http.HttpResponse;
import org.apache.http.client.fluent.Request;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Paths;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
public class THttpDTest {
private static final ExecutorService EXECUTOR = Executors.newSingleThreadExecutor();
private static final THttpD DAEMON;
static {
try {
DAEMON = new THttpD(Paths.get("src", "test", "resources"), 8080);
} catch (IOException e) {
throw new AssertionError(e);
}
}
@BeforeClass
public static void startServer() {
EXECUTOR.execute(DAEMON);
}
@AfterClass
public static void stopServer() throws IOException {
try {
DAEMON.stop();
} finally {
EXECUTOR.shutdown();
}
}
@Test
public void testGetNotFound() throws IOException {
HttpResponse httpResponse = Request.Get(URI.create("http://localhost:8080/not-existing")).execute().returnResponse();
assertThat(httpResponse.getStatusLine().getStatusCode(), is(404));
assertThat(httpResponse.getStatusLine().getReasonPhrase(), is("Not Found"));
}
@Test
public void testHeadNotFound() throws IOException {
HttpResponse httpResponse = Request.Head(URI.create("http://localhost:8080/not-existing")).execute().returnResponse();
assertThat(httpResponse.getStatusLine().getStatusCode(), is(404));
assertThat(httpResponse.getStatusLine().getReasonPhrase(), is("Not Found"));
}
@Test
public void testPutNotSupported() throws IOException {
HttpResponse httpResponse = Request.Put(URI.create("http://localhost:8080/org/sfj/colors.json")).execute().returnResponse();
assertThat(httpResponse.getStatusLine().getStatusCode(), is(501));
assertThat(httpResponse.getStatusLine().getReasonPhrase(), is("Not Implemented"));
}
@Test
public void testGetSucceeds() throws IOException {
HttpResponse httpResponse = Request.Get(URI.create("http://localhost:8080/org/sfj/colors.json")).execute().returnResponse();
assertThat(httpResponse.getStatusLine().getStatusCode(), is(200));
assertThat(httpResponse.getStatusLine().getReasonPhrase(), is("OK"));
}
@Test
public void testHeadSucceeds() throws IOException, InterruptedException {
HttpResponse httpResponse = Request.Head(URI.create("http://localhost:8080/org/sfj/colors.json")).execute().returnResponse();
assertThat(httpResponse.getStatusLine().getStatusCode(), is(200));
assertThat(httpResponse.getStatusLine().getReasonPhrase(), is("OK"));
}
}