aboutsummaryrefslogtreecommitdiff
path: root/core/src/test/java/fi/iki/elonen/HttpServerTest.java
blob: e3f856f3863c1af9695cf1f3b53b5ae117091377 (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package fi.iki.elonen;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;

/**
 * @author Paul S. Hawke (paul.hawke@gmail.com)
 *         On: 3/10/13 at 8:32 PM
 */
public class HttpServerTest {
    public static final String URI = "http://www.myserver.org/pub/WWW/someFile.html";
    protected TestServer testServer;
    private TestTempFileManager tempFileManager;

    @Before
    public void setUp() {
        testServer = new TestServer();
        tempFileManager = new TestTempFileManager();
    }

    @After
    public void tearDown() {
        tempFileManager._clear();
    }

    @Test
    public void testServerExists() {
        assertNotNull(testServer);
    }

    protected void assertResponse(ByteArrayOutputStream outputStream, String[] expected) throws IOException {
        List<String> lines = getOutputLines(outputStream);
        assertLinesOfText(expected, lines);
    }

    protected void assertLinesOfText(String[] expected, List<String> lines) {
        assertEquals(expected.length, lines.size());
        for (int i = 0; i < expected.length; i++) {
            String line = lines.get(i);
            assertTrue("Output line " + i + " doesn't match expectation.\n" +
                    "  Output: " + line + "\n" +
                    "Expected: " + expected[i], line.matches(expected[i]));
        }
    }

    protected ByteArrayOutputStream invokeServer(String request) {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(request.getBytes());
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        NanoHTTPD.HTTPSession session = testServer.createSession(tempFileManager, inputStream, outputStream);
        session.execute();
        return outputStream;
    }

    protected List<String> getOutputLines(ByteArrayOutputStream outputStream) throws IOException {
        BufferedReader reader = new BufferedReader(new StringReader(outputStream.toString()));
        return readLinesFromFile(reader);
    }

    protected List<String> readLinesFromFile(BufferedReader reader) throws IOException {
        List<String> lines = new ArrayList<String>();
        String line = "";
        while (line != null) {
            line = reader.readLine();
            if (line != null) {
                lines.add(line.trim());
            }
        }
        return lines;
    }

    public static class TestTempFileManager extends NanoHTTPD.DefaultTempFileManager {
        public void _clear() {
            super.clear();
        }

        @Override
        public void clear() {
            // ignore
        }
    }

    public static class TestServer extends NanoHTTPD {
        public Response response = new Response("");
        public String uri;
        public Method method;
        public Map<String, String> header;
        public Map<String, String> parms;
        public Map<String, String> files;
        public Map<String, List<String>> decodedParamters;
        public Map<String, List<String>> decodedParamtersFromParameter;

        public TestServer() {
            super(8080);
        }

        public HTTPSession createSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream) {
            return new HTTPSession(tempFileManager, inputStream, outputStream);
        }

        @Override
        public Response serve(String uri, Method method, Map<String, String> header, Map<String, String> parms, Map<String, String> files) {
            this.uri = uri;
            this.method = method;
            this.header = header;
            this.parms = parms;
            this.files = files;
            this.decodedParamtersFromParameter = decodeParameters(parms);
            this.decodedParamters = decodeParameters(parms.get("NanoHttpd.QUERY_STRING"));
            return response;
        }

        @Override
        public String decodePercent(String str) {
            return super.decodePercent(str);
        }
    }
}