Skip to content

Commit 7c1ea78

Browse files
committed
Use URLs as-is to prevent double percent-encoding
Closes gh-46
1 parent 5f513ec commit 7c1ea78

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

Diff for: src/main/java/io/spring/issuebot/github/GitHubTemplate.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ private <T> Page<T> getPage(String url, Class<T[]> type) {
139139
if (!StringUtils.hasText(url)) {
140140
return null;
141141
}
142-
ResponseEntity<T[]> contents = this.rest.getForEntity(url, type);
142+
ResponseEntity<T[]> contents = this.rest.getForEntity(URI.create(url), type);
143143
return new StandardPage<>(Arrays.asList(contents.getBody()), () -> getPage(getNextUrl(contents), type));
144144
}
145145

@@ -169,7 +169,7 @@ public Issue removeLabel(Issue issue, String labelName) {
169169
catch (URISyntaxException ex) {
170170
throw new RuntimeException(ex);
171171
}
172-
ResponseEntity<Label[]> response = this.rest.exchange(new RequestEntity<Void>(HttpMethod.DELETE,
172+
ResponseEntity<Label[]> response = this.rest.exchange(new RequestEntity<>(HttpMethod.DELETE,
173173
URI.create(issue.getLabelsUrl().replace("{/name}", "/" + encodedName))), Label[].class);
174174
if (response.getStatusCode() != HttpStatus.OK) {
175175
log.warn("Failed to remove label from issue. Response status: " + response.getStatusCode());

Diff for: src/test/java/io/spring/issuebot/github/GitHubTemplateTests.java

+30-12
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ class GitHubTemplateTests {
5454
private final MockRestServiceServer server = MockRestServiceServer
5555
.createServer((RestTemplate) this.gitHub.getRestOperations());
5656

57+
@Test
58+
void notSureYet() {
59+
new GitHubTemplate(null, null, new RegexLinkParser()).getIssues("spring-projects", "spring-boot").next();
60+
}
61+
5762
@Test
5863
void noIssues() {
5964
this.server.expect(requestTo("https://api.github.com/repos/org/repo/issues"))
@@ -78,12 +83,10 @@ void singlePageOfIssues() {
7883

7984
@Test
8085
void multiplePagesOfIssues() {
81-
HttpHeaders headers = new HttpHeaders();
82-
headers.set("Link", "<page-two>; rel=\"next\"");
8386
this.server.expect(requestTo("https://api.github.com/repos/org/repo/issues"))
8487
.andExpect(method(HttpMethod.GET))
8588
.andExpect(basicAuth())
86-
.andRespond(withResource("issues-page-one.json", "Link:<page-two>; rel=\"next\""));
89+
.andRespond(withResource("issues-page-one.json", "Link:</page-two>; rel=\"next\""));
8790
this.server.expect(requestTo("/page-two"))
8891
.andExpect(method(HttpMethod.GET))
8992
.andExpect(basicAuth())
@@ -95,6 +98,23 @@ void multiplePagesOfIssues() {
9598
assertThat(pageTwo.getContent()).hasSize(15);
9699
}
97100

101+
@Test
102+
void multiplePagesOfIssuesWithPercentEncodedLink() {
103+
this.server.expect(requestTo("https://api.github.com/repos/org/repo/issues"))
104+
.andExpect(method(HttpMethod.GET))
105+
.andExpect(basicAuth())
106+
.andRespond(withResource("issues-page-one.json", "Link:</page-two%3D%3D>; rel=\"next\""));
107+
this.server.expect(requestTo("/page-two%3D%3D"))
108+
.andExpect(method(HttpMethod.GET))
109+
.andExpect(basicAuth())
110+
.andRespond(withResource("issues-page-two.json"));
111+
Page<Issue> pageOne = this.gitHub.getIssues("org", "repo");
112+
assertThat(pageOne.getContent()).hasSize(15);
113+
Page<Issue> pageTwo = pageOne.next();
114+
assertThat(pageTwo).isNotNull();
115+
assertThat(pageTwo.getContent()).hasSize(15);
116+
}
117+
98118
@Test
99119
void rateLimited() {
100120
long reset = System.currentTimeMillis();
@@ -117,7 +137,7 @@ void noComments() {
117137
.andExpect(basicAuth())
118138
.andRespond(withSuccess("[]", MediaType.APPLICATION_JSON));
119139
Page<Comment> comments = this.gitHub
120-
.getComments(new Issue(null, "commentsUrl", null, null, null, null, null, null));
140+
.getComments(new Issue(null, "/commentsUrl", null, null, null, null, null, null));
121141
assertThat(comments.getContent()).isEmpty();
122142
assertThat(comments.next()).isNull();
123143
}
@@ -129,7 +149,7 @@ void singlePageOfComments() {
129149
.andExpect(basicAuth())
130150
.andRespond(withResource("comments-page-one.json"));
131151
Page<Comment> comments = this.gitHub
132-
.getComments(new Issue(null, "commentsUrl", null, null, null, null, null, null));
152+
.getComments(new Issue(null, "/commentsUrl", null, null, null, null, null, null));
133153
assertThat(comments.getContent()).hasSize(17);
134154
assertThat(comments.next()).isNull();
135155
}
@@ -141,13 +161,13 @@ void multiplePagesOfComments() {
141161
this.server.expect(requestTo("/commentsUrl"))
142162
.andExpect(method(HttpMethod.GET))
143163
.andExpect(basicAuth())
144-
.andRespond(withResource("comments-page-one.json", "Link:<page-two>; rel=\"next\""));
164+
.andRespond(withResource("comments-page-one.json", "Link:</page-two>; rel=\"next\""));
145165
this.server.expect(requestTo("/page-two"))
146166
.andExpect(method(HttpMethod.GET))
147167
.andExpect(basicAuth())
148168
.andRespond(withResource("comments-page-two.json"));
149169
Page<Comment> pageOne = this.gitHub
150-
.getComments(new Issue(null, "commentsUrl", null, null, null, null, null, null));
170+
.getComments(new Issue(null, "/commentsUrl", null, null, null, null, null, null));
151171
assertThat(pageOne.getContent()).hasSize(17);
152172
Page<Comment> pageTwo = pageOne.next();
153173
assertThat(pageTwo).isNotNull();
@@ -206,24 +226,22 @@ void singlePageOfEvents() {
206226
.andExpect(method(HttpMethod.GET))
207227
.andExpect(basicAuth())
208228
.andRespond(withResource("events-page-one.json"));
209-
Page<Event> events = this.gitHub.getEvents(new Issue(null, null, "eventsUrl", null, null, null, null, null));
229+
Page<Event> events = this.gitHub.getEvents(new Issue(null, null, "/eventsUrl", null, null, null, null, null));
210230
assertThat(events.getContent()).hasSize(12);
211231
assertThat(events.next()).isNull();
212232
}
213233

214234
@Test
215235
void multiplePagesOfEvents() {
216-
HttpHeaders headers = new HttpHeaders();
217-
headers.set("Link", "<page-two>; rel=\"next\"");
218236
this.server.expect(requestTo("/eventsUrl"))
219237
.andExpect(method(HttpMethod.GET))
220238
.andExpect(basicAuth())
221-
.andRespond(withResource("events-page-one.json", "Link:<page-two>; rel=\"next\""));
239+
.andRespond(withResource("events-page-one.json", "Link:</page-two>; rel=\"next\""));
222240
this.server.expect(requestTo("/page-two"))
223241
.andExpect(method(HttpMethod.GET))
224242
.andExpect(basicAuth())
225243
.andRespond(withResource("events-page-two.json"));
226-
Page<Event> pageOne = this.gitHub.getEvents(new Issue(null, null, "eventsUrl", null, null, null, null, null));
244+
Page<Event> pageOne = this.gitHub.getEvents(new Issue(null, null, "/eventsUrl", null, null, null, null, null));
227245
assertThat(pageOne.getContent()).hasSize(12);
228246
Page<Event> pageTwo = pageOne.next();
229247
assertThat(pageTwo).isNotNull();

0 commit comments

Comments
 (0)