summaryrefslogtreecommitdiffstats
blob: 257bab741efd361dc804460429320c69aa4140c8 (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
/*
 * Copyright (c) 2012 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */
/*
 * Copyright (c) 2015 Tarun Gupta.
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

package com.isode.stroke.base;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.BeforeClass;
import org.junit.Test;
import com.isode.stroke.base.URL;

public class URLTest {

	@Test
	public void testFromString() {
		URL url = URL.fromString("http://foo.bar/baz/bam");

		assertEquals("http", url.getScheme());
		assertEquals("foo.bar", url.getHost());
		assertTrue(url.getPort() == null);
		assertEquals("/baz/bam", url.getPath());
	}

	@Test
	public void testFromString_WithoutPath() {
		URL url = URL.fromString("http://foo.bar");

		assertEquals("http", url.getScheme());
		assertEquals("foo.bar", url.getHost());
		assertTrue(url.getPort() == null);
		assertEquals("", url.getPath());
	}

	@Test
	public void testFromString_WithRootPath() {
		URL url = URL.fromString("http://foo.bar/");

		assertEquals("http", url.getScheme());
		assertEquals("foo.bar", url.getHost());
		assertTrue(url.getPort() == null);
		assertEquals("/", url.getPath());
	}

	@Test
	public void testFromString_WithPort() {
		URL url = URL.fromString("http://foo.bar:1234/baz/bam");

		assertEquals("http", url.getScheme());
		assertEquals("foo.bar", url.getHost());
		assertEquals(Integer.valueOf(1234), url.getPort());
		assertEquals("/baz/bam", url.getPath());
	}

	@Test
	public void testFromString_WithPortOnePartPath() {
		URL url = URL.fromString("http://foo.bar:11440/http-bind/");

		assertEquals("http", url.getScheme());
		assertEquals("foo.bar", url.getHost());
		assertEquals(Integer.valueOf(11440), url.getPort());
		assertEquals("/http-bind/", url.getPath());
	}

	@Test
	public void testFromString_WithPortWithoutPath() {
		URL url = URL.fromString("http://foo.bar:1234");

		assertEquals("http", url.getScheme());
		assertEquals("foo.bar", url.getHost());
		assertEquals(Integer.valueOf(1234), url.getPort());
		assertEquals("", url.getPath());
	}

	@Test
	public void testFromString_WithUserInfo() {
		URL url = URL.fromString("http://user:pass@foo.bar/baz/bam");

		assertEquals("http", url.getScheme());
		assertEquals("foo.bar", url.getHost());
		assertEquals("/baz/bam", url.getPath());
	}


	@Test
	public void testFromString_NonASCIIHost() {
		URL url = URL.fromString("http://www.tron%C3%A7on.be/baz/bam");

		assertEquals("www.tron\u00E7on.be", url.getHost());
	}

	@Test
	public void testFromString_NonASCIIPath() {
		URL url = URL.fromString("http://foo.bar/baz/tron%C3%A7on/bam");

		assertEquals("/baz/tron\u00E7on/bam", url.getPath());
	}

	@Test
	public void testToString() {
		assertEquals("http://foo.bar/baz/bam", new URL("http", "foo.bar", "/baz/bam").toString());
	}

	@Test
	public void testToString_WithPort() {
		assertEquals("http://foo.bar:1234/baz/bam", new URL("http", "foo.bar", 1234, "/baz/bam").toString());
	}
}