14
14
package com .google .devtools .build .lib .bazel .repository .downloader ;
15
15
16
16
import static com .google .common .truth .Truth .assertThat ;
17
+ import static org .junit .Assert .fail ;
17
18
18
19
import com .google .common .collect .ImmutableList ;
19
20
import java .io .StringReader ;
20
- import java .net .MalformedURLException ;
21
21
import java .net .URL ;
22
22
import java .util .List ;
23
+ import net .starlark .java .syntax .Location ;
23
24
import org .junit .Test ;
24
25
import org .junit .runner .RunWith ;
25
26
import org .junit .runners .JUnit4 ;
29
30
public class UrlRewriterTest {
30
31
31
32
@ Test
32
- public void byDefaultTheUrlRewriterDoesNothing () throws MalformedURLException {
33
- UrlRewriter munger = new UrlRewriter (str -> {}, new StringReader ("" ));
33
+ public void byDefaultTheUrlRewriterDoesNothing () throws Exception {
34
+ UrlRewriter munger = new UrlRewriter (str -> {}, "/dev/null" , new StringReader ("" ));
34
35
35
36
List <URL > urls = ImmutableList .of (new URL ("http://example.com" ));
36
37
List <URL > amended = munger .amend (urls );
@@ -39,9 +40,9 @@ public void byDefaultTheUrlRewriterDoesNothing() throws MalformedURLException {
39
40
}
40
41
41
42
@ Test
42
- public void shouldBeAbleToBlockParticularHostsRegardlessOfScheme () throws MalformedURLException {
43
+ public void shouldBeAbleToBlockParticularHostsRegardlessOfScheme () throws Exception {
43
44
String config = "block example.com" ;
44
- UrlRewriter munger = new UrlRewriter (str -> {}, new StringReader (config ));
45
+ UrlRewriter munger = new UrlRewriter (str -> {}, "/dev/null" , new StringReader (config ));
45
46
46
47
List <URL > urls =
47
48
ImmutableList .of (
@@ -54,9 +55,9 @@ public void shouldBeAbleToBlockParticularHostsRegardlessOfScheme() throws Malfor
54
55
}
55
56
56
57
@ Test
57
- public void shouldAllowAUrlToBeRewritten () throws MalformedURLException {
58
+ public void shouldAllowAUrlToBeRewritten () throws Exception {
58
59
String config = "rewrite example.com/foo/(.*) mycorp.com/$1/foo" ;
59
- UrlRewriter munger = new UrlRewriter (str -> {}, new StringReader (config ));
60
+ UrlRewriter munger = new UrlRewriter (str -> {}, "/dev/null" , new StringReader (config ));
60
61
61
62
List <URL > urls = ImmutableList .of (new URL ("https://example.com/foo/bar" ));
62
63
List <URL > amended = munger .amend (urls );
@@ -65,11 +66,11 @@ public void shouldAllowAUrlToBeRewritten() throws MalformedURLException {
65
66
}
66
67
67
68
@ Test
68
- public void rewritesCanExpandToMoreThanOneUrl () throws MalformedURLException {
69
+ public void rewritesCanExpandToMoreThanOneUrl () throws Exception {
69
70
String config =
70
71
"rewrite example.com/foo/(.*) mycorp.com/$1/somewhere\n "
71
72
+ "rewrite example.com/foo/(.*) mycorp.com/$1/elsewhere" ;
72
- UrlRewriter munger = new UrlRewriter (str -> {}, new StringReader (config ));
73
+ UrlRewriter munger = new UrlRewriter (str -> {}, "/dev/null" , new StringReader (config ));
73
74
74
75
List <URL > urls = ImmutableList .of (new URL ("https://example.com/foo/bar" ));
75
76
List <URL > amended = munger .amend (urls );
@@ -80,10 +81,10 @@ public void rewritesCanExpandToMoreThanOneUrl() throws MalformedURLException {
80
81
}
81
82
82
83
@ Test
83
- public void shouldBlockAllUrlsOtherThanSpecificOnes () throws MalformedURLException {
84
+ public void shouldBlockAllUrlsOtherThanSpecificOnes () throws Exception {
84
85
String config = "" + "block *\n " + "allow example.com" ;
85
86
86
- UrlRewriter munger = new UrlRewriter (str -> {}, new StringReader (config ));
87
+ UrlRewriter munger = new UrlRewriter (str -> {}, "/dev/null" , new StringReader (config ));
87
88
88
89
List <URL > urls =
89
90
ImmutableList .of (
@@ -98,15 +99,15 @@ public void shouldBlockAllUrlsOtherThanSpecificOnes() throws MalformedURLExcepti
98
99
}
99
100
100
101
@ Test
101
- public void commentsArePrecededByTheHashCharacter () throws MalformedURLException {
102
+ public void commentsArePrecededByTheHashCharacter () throws Exception {
102
103
String config =
103
104
""
104
105
+ "# Block everything\n "
105
106
+ "block *\n "
106
107
+ "# But allow example.com\n "
107
108
+ "allow example.com" ;
108
109
109
- UrlRewriter munger = new UrlRewriter (str -> {}, new StringReader (config ));
110
+ UrlRewriter munger = new UrlRewriter (str -> {}, "/dev/null" , new StringReader (config ));
110
111
111
112
List <URL > urls = ImmutableList .of (new URL ("https://foo.com" ), new URL ("https://example.com" ));
112
113
List <URL > amended = munger .amend (urls );
@@ -115,43 +116,43 @@ public void commentsArePrecededByTheHashCharacter() throws MalformedURLException
115
116
}
116
117
117
118
@ Test
118
- public void allowListAppliesToSubdomainsToo () throws MalformedURLException {
119
+ public void allowListAppliesToSubdomainsToo () throws Exception {
119
120
String config = "" + "block *\n " + "allow example.com" ;
120
121
121
- UrlRewriter munger = new UrlRewriter (str -> {}, new StringReader (config ));
122
+ UrlRewriter munger = new UrlRewriter (str -> {}, "/dev/null" , new StringReader (config ));
122
123
123
124
List <URL > amended = munger .amend (ImmutableList .of (new URL ("https://subdomain.example.com" )));
124
125
125
126
assertThat (amended ).containsExactly (new URL ("https://subdomain.example.com" ));
126
127
}
127
128
128
129
@ Test
129
- public void blockListAppliesToSubdomainsToo () throws MalformedURLException {
130
+ public void blockListAppliesToSubdomainsToo () throws Exception {
130
131
String config = "block example.com" ;
131
132
132
- UrlRewriter munger = new UrlRewriter (str -> {}, new StringReader (config ));
133
+ UrlRewriter munger = new UrlRewriter (str -> {}, "/dev/null" , new StringReader (config ));
133
134
134
135
List <URL > amended = munger .amend (ImmutableList .of (new URL ("https://subdomain.example.com" )));
135
136
136
137
assertThat (amended ).isEmpty ();
137
138
}
138
139
139
140
@ Test
140
- public void emptyLinesAreFine () throws MalformedURLException {
141
+ public void emptyLinesAreFine () throws Exception {
141
142
String config = "" + "\n " + " \n " + "block *\n " + "\t \n " + "allow example.com" ;
142
143
143
- UrlRewriter munger = new UrlRewriter (str -> {}, new StringReader (config ));
144
+ UrlRewriter munger = new UrlRewriter (str -> {}, "/dev/null" , new StringReader (config ));
144
145
145
146
List <URL > amended = munger .amend (ImmutableList .of (new URL ("https://subdomain.example.com" )));
146
147
147
148
assertThat (amended ).containsExactly (new URL ("https://subdomain.example.com" ));
148
149
}
149
150
150
151
@ Test
151
- public void rewritingUrlsIsAppliedBeforeBlocking () throws MalformedURLException {
152
+ public void rewritingUrlsIsAppliedBeforeBlocking () throws Exception {
152
153
String config = "" + "block bad.com\n " + "rewrite bad.com/foo/(.*) mycorp.com/$1" ;
153
154
154
- UrlRewriter munger = new UrlRewriter (str -> {}, new StringReader (config ));
155
+ UrlRewriter munger = new UrlRewriter (str -> {}, "/dev/null" , new StringReader (config ));
155
156
156
157
List <URL > amended =
157
158
munger .amend (
@@ -161,16 +162,27 @@ public void rewritingUrlsIsAppliedBeforeBlocking() throws MalformedURLException
161
162
}
162
163
163
164
@ Test
164
- public void rewritingUrlsIsAppliedBeforeAllowing () throws MalformedURLException {
165
+ public void rewritingUrlsIsAppliedBeforeAllowing () throws Exception {
165
166
String config =
166
167
"" + "block *\n " + "allow mycorp.com\n " + "rewrite bad.com/foo/(.*) mycorp.com/$1" ;
167
168
168
- UrlRewriter munger = new UrlRewriter (str -> {}, new StringReader (config ));
169
+ UrlRewriter munger = new UrlRewriter (str -> {}, "/dev/null" , new StringReader (config ));
169
170
170
171
List <URL > amended =
171
172
munger .amend (
172
173
ImmutableList .of (new URL ("https://www.bad.com" ), new URL ("https://bad.com/foo/bar" )));
173
174
174
175
assertThat (amended ).containsExactly (new URL ("https://mycorp.com/bar" ));
175
176
}
177
+
178
+ @ Test
179
+ public void parseError () throws Exception {
180
+ String config = "#comment\n hello" ;
181
+ try {
182
+ new UrlRewriterConfig ("/some/file" , new StringReader (config ));
183
+ fail ();
184
+ } catch (UrlRewriterParseException e ) {
185
+ assertThat (e .getLocation ()).isEqualTo (Location .fromFileLineColumn ("/some/file" , 2 , 0 ));
186
+ }
187
+ }
176
188
}
0 commit comments