1414package com .google .devtools .build .lib .bazel .repository .downloader ;
1515
1616import static com .google .common .truth .Truth .assertThat ;
17+ import static org .junit .Assert .fail ;
1718
1819import com .google .common .collect .ImmutableList ;
1920import java .io .StringReader ;
20- import java .net .MalformedURLException ;
2121import java .net .URL ;
2222import java .util .List ;
23+ import net .starlark .java .syntax .Location ;
2324import org .junit .Test ;
2425import org .junit .runner .RunWith ;
2526import org .junit .runners .JUnit4 ;
2930public class UrlRewriterTest {
3031
3132 @ 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 ("" ));
3435
3536 List <URL > urls = ImmutableList .of (new URL ("http://example.com" ));
3637 List <URL > amended = munger .amend (urls );
@@ -39,9 +40,9 @@ public void byDefaultTheUrlRewriterDoesNothing() throws MalformedURLException {
3940 }
4041
4142 @ Test
42- public void shouldBeAbleToBlockParticularHostsRegardlessOfScheme () throws MalformedURLException {
43+ public void shouldBeAbleToBlockParticularHostsRegardlessOfScheme () throws Exception {
4344 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 ));
4546
4647 List <URL > urls =
4748 ImmutableList .of (
@@ -54,9 +55,9 @@ public void shouldBeAbleToBlockParticularHostsRegardlessOfScheme() throws Malfor
5455 }
5556
5657 @ Test
57- public void shouldAllowAUrlToBeRewritten () throws MalformedURLException {
58+ public void shouldAllowAUrlToBeRewritten () throws Exception {
5859 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 ));
6061
6162 List <URL > urls = ImmutableList .of (new URL ("https://example.com/foo/bar" ));
6263 List <URL > amended = munger .amend (urls );
@@ -65,11 +66,11 @@ public void shouldAllowAUrlToBeRewritten() throws MalformedURLException {
6566 }
6667
6768 @ Test
68- public void rewritesCanExpandToMoreThanOneUrl () throws MalformedURLException {
69+ public void rewritesCanExpandToMoreThanOneUrl () throws Exception {
6970 String config =
7071 "rewrite example.com/foo/(.*) mycorp.com/$1/somewhere\n "
7172 + "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 ));
7374
7475 List <URL > urls = ImmutableList .of (new URL ("https://example.com/foo/bar" ));
7576 List <URL > amended = munger .amend (urls );
@@ -80,10 +81,10 @@ public void rewritesCanExpandToMoreThanOneUrl() throws MalformedURLException {
8081 }
8182
8283 @ Test
83- public void shouldBlockAllUrlsOtherThanSpecificOnes () throws MalformedURLException {
84+ public void shouldBlockAllUrlsOtherThanSpecificOnes () throws Exception {
8485 String config = "" + "block *\n " + "allow example.com" ;
8586
86- UrlRewriter munger = new UrlRewriter (str -> {}, new StringReader (config ));
87+ UrlRewriter munger = new UrlRewriter (str -> {}, "/dev/null" , new StringReader (config ));
8788
8889 List <URL > urls =
8990 ImmutableList .of (
@@ -98,15 +99,15 @@ public void shouldBlockAllUrlsOtherThanSpecificOnes() throws MalformedURLExcepti
9899 }
99100
100101 @ Test
101- public void commentsArePrecededByTheHashCharacter () throws MalformedURLException {
102+ public void commentsArePrecededByTheHashCharacter () throws Exception {
102103 String config =
103104 ""
104105 + "# Block everything\n "
105106 + "block *\n "
106107 + "# But allow example.com\n "
107108 + "allow example.com" ;
108109
109- UrlRewriter munger = new UrlRewriter (str -> {}, new StringReader (config ));
110+ UrlRewriter munger = new UrlRewriter (str -> {}, "/dev/null" , new StringReader (config ));
110111
111112 List <URL > urls = ImmutableList .of (new URL ("https://foo.com" ), new URL ("https://example.com" ));
112113 List <URL > amended = munger .amend (urls );
@@ -115,43 +116,43 @@ public void commentsArePrecededByTheHashCharacter() throws MalformedURLException
115116 }
116117
117118 @ Test
118- public void allowListAppliesToSubdomainsToo () throws MalformedURLException {
119+ public void allowListAppliesToSubdomainsToo () throws Exception {
119120 String config = "" + "block *\n " + "allow example.com" ;
120121
121- UrlRewriter munger = new UrlRewriter (str -> {}, new StringReader (config ));
122+ UrlRewriter munger = new UrlRewriter (str -> {}, "/dev/null" , new StringReader (config ));
122123
123124 List <URL > amended = munger .amend (ImmutableList .of (new URL ("https://subdomain.example.com" )));
124125
125126 assertThat (amended ).containsExactly (new URL ("https://subdomain.example.com" ));
126127 }
127128
128129 @ Test
129- public void blockListAppliesToSubdomainsToo () throws MalformedURLException {
130+ public void blockListAppliesToSubdomainsToo () throws Exception {
130131 String config = "block example.com" ;
131132
132- UrlRewriter munger = new UrlRewriter (str -> {}, new StringReader (config ));
133+ UrlRewriter munger = new UrlRewriter (str -> {}, "/dev/null" , new StringReader (config ));
133134
134135 List <URL > amended = munger .amend (ImmutableList .of (new URL ("https://subdomain.example.com" )));
135136
136137 assertThat (amended ).isEmpty ();
137138 }
138139
139140 @ Test
140- public void emptyLinesAreFine () throws MalformedURLException {
141+ public void emptyLinesAreFine () throws Exception {
141142 String config = "" + "\n " + " \n " + "block *\n " + "\t \n " + "allow example.com" ;
142143
143- UrlRewriter munger = new UrlRewriter (str -> {}, new StringReader (config ));
144+ UrlRewriter munger = new UrlRewriter (str -> {}, "/dev/null" , new StringReader (config ));
144145
145146 List <URL > amended = munger .amend (ImmutableList .of (new URL ("https://subdomain.example.com" )));
146147
147148 assertThat (amended ).containsExactly (new URL ("https://subdomain.example.com" ));
148149 }
149150
150151 @ Test
151- public void rewritingUrlsIsAppliedBeforeBlocking () throws MalformedURLException {
152+ public void rewritingUrlsIsAppliedBeforeBlocking () throws Exception {
152153 String config = "" + "block bad.com\n " + "rewrite bad.com/foo/(.*) mycorp.com/$1" ;
153154
154- UrlRewriter munger = new UrlRewriter (str -> {}, new StringReader (config ));
155+ UrlRewriter munger = new UrlRewriter (str -> {}, "/dev/null" , new StringReader (config ));
155156
156157 List <URL > amended =
157158 munger .amend (
@@ -161,16 +162,27 @@ public void rewritingUrlsIsAppliedBeforeBlocking() throws MalformedURLException
161162 }
162163
163164 @ Test
164- public void rewritingUrlsIsAppliedBeforeAllowing () throws MalformedURLException {
165+ public void rewritingUrlsIsAppliedBeforeAllowing () throws Exception {
165166 String config =
166167 "" + "block *\n " + "allow mycorp.com\n " + "rewrite bad.com/foo/(.*) mycorp.com/$1" ;
167168
168- UrlRewriter munger = new UrlRewriter (str -> {}, new StringReader (config ));
169+ UrlRewriter munger = new UrlRewriter (str -> {}, "/dev/null" , new StringReader (config ));
169170
170171 List <URL > amended =
171172 munger .amend (
172173 ImmutableList .of (new URL ("https://www.bad.com" ), new URL ("https://bad.com/foo/bar" )));
173174
174175 assertThat (amended ).containsExactly (new URL ("https://mycorp.com/bar" ));
175176 }
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+ }
176188}
0 commit comments