summaryrefslogtreecommitdiffstats
path: root/container-accesslogging/src/test/java/com/yahoo/container/logging/JSONLogTestCase.java
blob: ceafbc18272f8a05c9fefee3a80e184f23085b93 (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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.logging;

import org.junit.Test;

import java.net.URI;

import static org.junit.Assert.assertEquals;


/**
 * @author frodelu
 */
public class JSONLogTestCase {

    private static String ipAddress = "152.200.54.243";
    private static final String EMPTY_REFERRER = "";
    private static final String EMPTY_USERAGENT = "";

    private AccessLogEntry newAccessLogEntry(final String query) {
        final AccessLogEntry entry = new AccessLogEntry();
        entry.setRawQuery("query="+query);
        entry.setRawPath("");
        entry.setIpV4Address(ipAddress);
        entry.setHttpMethod("GET");
        entry.setHttpVersion("HTTP/1.1");
        entry.setUserAgent("Mozilla/4.05 [en] (Win95; I)");
        entry.setHitCounts(new HitCounts(0, 10, 1234, 0, 10));
        entry.setHostString("localhost");
        entry.setStatusCode(200);
        entry.setTimeStamp(920880005023L);
        entry.setDurationBetweenRequestResponse(122);
        entry.setReturnedContentSize(9875);

        return entry;
    }

    private static URI newQueryUri(final String query) {
        return URI.create("http://localhost?query=" + query);
    }

    @Test
    public void test_json_log_entry() throws Exception {
        AccessLogEntry entry = newAccessLogEntry("test");

         String expectedOutput =
            "{\"ip\":\"152.200.54.243\"," +
            "\"time\":920880005.023," +
            "\"duration\":0.122," +
            "\"responsesize\":9875," +
            "\"code\":200," +
            "\"method\":\"GET\"," +
            "\"uri\":\"?query=test\"," +
            "\"version\":\"HTTP/1.1\"," +
            "\"agent\":\"Mozilla/4.05 [en] (Win95; I)\"," +
            "\"host\":\"localhost\"," +
            "\"scheme\":null," +
            "\"localport\":0," +
            "\"search\":{" +
            "\"totalhits\":1234," +
            "\"hits\":0" +
            "}" +
            "}";

        assertEquals(expectedOutput, new JSONFormatter(entry).format());
    }

    @Test
    public void test_with_keyvalues() {
        AccessLogEntry entry = newAccessLogEntry("test");
        entry.addKeyValue("singlevalue", "value1");
        entry.addKeyValue("multivalue", "value2");
        entry.addKeyValue("multivalue", "value3");

        String expectedOutput =
            "{\"ip\":\"152.200.54.243\"," +
            "\"time\":920880005.023," +
            "\"duration\":0.122," +
            "\"responsesize\":9875," +
            "\"code\":200," +
            "\"method\":\"GET\"," +
            "\"uri\":\"?query=test\"," +
            "\"version\":\"HTTP/1.1\"," +
            "\"agent\":\"Mozilla/4.05 [en] (Win95; I)\"," +
            "\"host\":\"localhost\"," +
            "\"scheme\":null," +
            "\"localport\":0," +
            "\"search\":{" +
            "\"totalhits\":1234," +
            "\"hits\":0" +
            "}," +
            "\"attributes\":{" +
            "\"singlevalue\":\"value1\"," +
            "\"multivalue\":[\"value2\",\"value3\"]}" +
            "}";

        assertEquals(expectedOutput, new JSONFormatter(entry).format());

    }

    @Test
    public void test_with_remoteaddrport() throws Exception {
        AccessLogEntry entry = newAccessLogEntry("test");

        // First test with only remote address and not port set
        entry.setRemoteAddress("FE80:0000:0000:0000:0202:B3FF:FE1E:8329");

        String expectedOutput =
            "{\"ip\":\"152.200.54.243\"," +
            "\"time\":920880005.023," +
            "\"duration\":0.122," +
            "\"responsesize\":9875," +
            "\"code\":200," +
            "\"method\":\"GET\"," +
            "\"uri\":\"?query=test\"," +
            "\"version\":\"HTTP/1.1\"," +
            "\"agent\":\"Mozilla/4.05 [en] (Win95; I)\"," +
            "\"host\":\"localhost\"," +
            "\"scheme\":null," +
            "\"localport\":0," +
            "\"remoteaddr\":\"FE80:0000:0000:0000:0202:B3FF:FE1E:8329\"," +
            "\"search\":{" +
            "\"totalhits\":1234," +
            "\"hits\":0" +
            "}" +
            "}";

        assertEquals(expectedOutput, new JSONFormatter(entry).format());

        // Add remote port and verify
        entry.setRemotePort(1234);

        expectedOutput =
            "{\"ip\":\"152.200.54.243\"," +
            "\"time\":920880005.023," +
            "\"duration\":0.122," +
            "\"responsesize\":9875," +
            "\"code\":200," +
            "\"method\":\"GET\"," +
            "\"uri\":\"?query=test\"," +
            "\"version\":\"HTTP/1.1\"," +
            "\"agent\":\"Mozilla/4.05 [en] (Win95; I)\"," +
            "\"host\":\"localhost\"," +
            "\"scheme\":null," +
            "\"localport\":0," +
            "\"remoteaddr\":\"FE80:0000:0000:0000:0202:B3FF:FE1E:8329\"," +
            "\"remoteport\":1234," +
            "\"search\":{" +
            "\"totalhits\":1234," +
            "\"hits\":0" +
            "}" +
            "}";

        assertEquals(expectedOutput, new JSONFormatter(entry).format());
    }

    @Test
    public void test_remote_address_same_as_ip_address() throws Exception {
        AccessLogEntry entry = newAccessLogEntry("test");
        AccessLogEntry entrywithremote = newAccessLogEntry("test");
        entrywithremote.setRemoteAddress(entry.getIpV4Address());

        assertEquals(new JSONFormatter(entry).format(), new JSONFormatter(entrywithremote).format());
    }

    @Test
    public void test_useragent_with_quotes() {
        final AccessLogEntry entry = new AccessLogEntry();
        entry.setRawQuery("query=test");
        entry.setRawPath("");
        entry.setIpV4Address(ipAddress);
        entry.setHttpMethod("GET");
        entry.setHttpVersion("HTTP/1.1");
        entry.setUserAgent("Mozilla/4.05 [en] (Win95; I; \"Best Browser Ever\")");
        entry.setHitCounts(new HitCounts(0, 10, 1234, 0, 10));
        entry.setHostString("localhost");
        entry.setStatusCode(200);
        entry.setTimeStamp(920880005023L);
        entry.setDurationBetweenRequestResponse(122);
        entry.setReturnedContentSize(9875);

        String expectedOutput =
            "{\"ip\":\"152.200.54.243\"," +
            "\"time\":920880005.023," +
            "\"duration\":0.122," +
            "\"responsesize\":9875," +
            "\"code\":200," +
            "\"method\":\"GET\"," +
            "\"uri\":\"?query=test\"," +
            "\"version\":\"HTTP/1.1\"," +
            "\"agent\":\"Mozilla/4.05 [en] (Win95; I; \\\"Best Browser Ever\\\")\"," +
            "\"host\":\"localhost\"," +
            "\"scheme\":null," +
            "\"localport\":0," +
            "\"search\":{" +
            "\"totalhits\":1234," +
            "\"hits\":0" +
            "}" +
            "}";

        assertEquals(expectedOutput, new JSONFormatter(entry).format());
    }

}