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
79
80
81
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.handler.test;
import com.yahoo.container.jdisc.HttpRequest;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.filedistribution.fileacquirer.MockFileAcquirer;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.concurrent.Executor;
import static org.junit.Assert.assertEquals;
/**
* @author Ulf Lilleengen
*/
public class MockServiceTest {
private final File testFile = new File("src/test/java/com/yahoo/container/handler/test/test.txt");
@Test
public void testHandlerTextFormat() throws InterruptedException, IOException {
HttpResponse response = runHandler(com.yahoo.jdisc.http.HttpRequest.Method.GET, "/foo/bar");
assertResponse(response, 200, "Hello\nThere!");
response = runHandler(com.yahoo.jdisc.http.HttpRequest.Method.GET, "http://my.host:8080/foo/bar?key1=foo&key2=bar");
assertResponse(response, 200, "With params!");
response = runHandler(com.yahoo.jdisc.http.HttpRequest.Method.PUT, "/bar");
assertResponse(response, 301, "My data is on a single line");
}
@Test
public void testNoHandlerFound() throws InterruptedException, IOException {
HttpResponse response = runHandler(com.yahoo.jdisc.http.HttpRequest.Method.DELETE, "/foo/bar");
assertEquals(404, response.getStatus());
assertResponseContents(response, "DELETE:/foo/bar was not found");
}
@Test(expected = IllegalArgumentException.class)
public void testUnknownFileType() throws InterruptedException, IOException {
runHandlerWithFile(com.yahoo.jdisc.http.HttpRequest.Method.GET, "", new File("nonexistant"));
}
@Test(expected = FileNotFoundException.class)
public void testExceptionResponse() throws InterruptedException, IOException {
runHandlerWithFile(com.yahoo.jdisc.http.HttpRequest.Method.GET, "", new File("nonexistant.txt"));
}
private void assertResponse(HttpResponse response, int expectedCode, String expectedMessage) throws IOException {
assertEquals(expectedCode, response.getStatus());
assertResponseContents(response, expectedMessage);
}
private void assertResponseContents(HttpResponse response, String expected) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
response.render(baos);
assertEquals(expected, baos.toString());
}
private HttpResponse runHandler(com.yahoo.jdisc.http.HttpRequest.Method method, String path) throws InterruptedException, IOException {
return runHandlerWithFile(method, path, testFile);
}
private HttpResponse runHandlerWithFile(com.yahoo.jdisc.http.HttpRequest.Method method, String path, File file) throws InterruptedException, IOException {
MockserviceConfig.Builder builder = new MockserviceConfig.Builder();
builder.file(file.getPath());
MockService handler = new MockService(new MockExecutor(), MockFileAcquirer.returnFile(file), new MockserviceConfig(builder), null);
return handler.handle(HttpRequest.createTestRequest(path, method));
}
private static class MockExecutor implements Executor {
@Override
public void execute(Runnable command) {
command.run();
}
}
}
|