aboutsummaryrefslogtreecommitdiffstats
path: root/container-integration-test/src/test/java/com/yahoo/search/query/gui/GUIHandlerTest.java
blob: 1c6b9c329fb5eb49c761f948829f011d65f4409d (plain) (blame)
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
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query.gui;

import com.yahoo.application.Networking;
import com.yahoo.application.container.JDisc;
import com.yahoo.application.container.handler.Request;
import com.yahoo.application.container.handler.Response;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;



/**
 * @author Henrik Høiness
 */

public class GUIHandlerTest {

    private JDisc container;

    @Before
    public void startContainer() {
        container = JDisc.fromServicesXml(servicesXml(), Networking.enable);
    }

    @After
    public void stopContainer() {
        /*
        try {
            Thread.sleep(100_000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        container.close();
    }

    @Test
    public void testRequest() throws Exception {
       assertResponse("/querybuilder/", "<!-- Copyright 2018 Yahoo Holdings.","text/html; charset=UTF-8", 200);
    }

    @Test
    public void testContentTypes() throws Exception{
        assertResponse("/querybuilder/_includes/css/vespa.css", "/**","text/css; charset=UTF-8", 200);
        assertResponse("/querybuilder/js/agency.js", "/*!","application/javascript; charset=UTF-8", 200);
        assertResponse("/querybuilder/img/reload.svg", "<?xml","image/svg+xml; charset=UTF-8", 200);
        assertResponse("/querybuilder/img/Vespa-V2.png", null,"image/png; charset=UTF-8", 200);
    }

    @Test
    public void testInvalidPath() throws Exception{
        assertResponse("/querybuilder/invalid_filepath", "{\"error-code\":\"NOT_FOUND\",\"message\":\"Nothing at path","application/json; charset=UTF-8", 404);
    }


    private void assertResponse(String path, String expectedStartString,  String expectedContentType, int expectedStatusCode) throws IOException {
        assertResponse(Request.Method.GET, path, expectedStartString,expectedContentType, expectedStatusCode);
    }

    private void assertResponse(Request.Method method, String path, String expectedStartString, String expectedContentType, int expectedStatusCode) throws IOException {
        Response response = container.handleRequest(new Request("http://localhost:8080" + path, new byte[0], method));
        Assert.assertEquals("Status code", expectedStatusCode, response.getStatus());
        Assert.assertEquals(expectedContentType, response.getHeaders().getFirst("Content-Type"));
        if(expectedStartString != null){
            Assert.assertTrue(response.getBodyAsString().startsWith(expectedStartString));
        }
    }

    private String servicesXml() {
        return "<jdisc version='1.0'>\n" +
                "  <handler id='com.yahoo.search.query.gui.GUIHandler'>\n" +
                "    <binding>http://*/querybuilder/*</binding>\n" +
                "  </handler>\n" +
                "  <http>\n" +
                "    <server id='default' port='8080'/>\n" +
                "  </http>\n" +
                "</jdisc>";
    }

}