-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathParserTest.java
More file actions
89 lines (70 loc) · 2.93 KB
/
ParserTest.java
File metadata and controls
89 lines (70 loc) · 2.93 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
79
80
81
82
83
84
85
86
87
88
89
package com.snowplowanalytics.refererparser;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import org.junit.Before;
import org.junit.Test;
import org.json.JSONException;
import org.json.JSONTokener;
import org.json.JSONObject;
import org.json.JSONArray;
public class ParserTest {
private Parser parser;
@Before
public void createParser() throws CorruptYamlException {
parser = new Parser();
}
@Test
public void refererTests() throws MalformedURLException, JSONException {
JSONTokener tok = new JSONTokener( ParserTest.class.getResourceAsStream("/referer-tests.json"));
JSONArray root = new JSONArray(tok);
for (int i = 0; i < root.length(); i++) {
JSONObject entry = (JSONObject) root.get(i);
Referer actual = parser.parse(new URL(entry.getString("uri")), "www.snowplowanalytics.com");
assertEquals(entry.getString("spec") + " medium", entry.getString("medium"), actual.medium.toString());
assertEquals(entry.getString("spec") + " source", entry.get("source"), actual.source);
assertEquals(entry.getString("spec") + " term", entry.get("term"), actual.term);
}
}
@Test
public void basicTests() throws URISyntaxException, MalformedURLException, JSONException{
//Test every signature.
String u = "http://www.google.com/search?q=gateway+oracle+cards+denise+linn&hl=en&client=safari";
String ref = "www.example.com";
String expected = "{medium: search, source: Google, term: gateway oracle cards denise linn}";
//URI, URI
Referer result = parser.parse(new URI(u), new URI("http://"+ref));
assertEquals("URI, URI", expected, result.toString());
//String, URI
result = parser.parse(u, new URI("http://"+ref));
assertEquals("String, URI", expected, result.toString());
//String, String
result = parser.parse(u, ref);
assertEquals("String, String", expected, result.toString());
//URI, String
result = parser.parse(new URI(u), ref);
assertEquals("URI, String", expected, result.toString());
//URL, String
result = parser.parse(new URL(u), ref);
assertEquals("URL, String", expected, result.toString());
}
@Test
public void schemeTest() throws URISyntaxException {
// Test different supported schemes
String ref = "m.facebook.com";
String u = "www.example.com";
String expected = "{medium: social, source: Facebook, term: null}";
// http
Referer result = parser.parse(new URI("http://"+ref), new URI(u));
assertEquals("http", expected, result.toString());
// https
result = parser.parse(new URI("https://"+ref), new URI(u));
assertEquals("https", expected, result.toString());
// android-app
result = parser.parse(new URI("android-app://"+ref), new URI(u));
assertEquals("android-app", expected, result.toString());
}
}