Skip to content

Commit 7474cce

Browse files
committed
/series/import/request: encode URL to percent encoding prior its persisting.
The quote from URL javadoc: % Note, the URI class does perform escaping of its component fields in certain circumstances. The recommended way to manage the encoding and decoding of URLs is to use URI, and to convert between these two classes using toURI() and URI.toURL(). The URLEncoder and URLDecoder classes can also be used, but only for HTML form encoding, which is not the same as the encoding scheme defined in RFC2396. % Fix #772
1 parent 27d5637 commit 7474cce

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

src/main/java/ru/mystamps/web/service/SeriesImportServiceImpl.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
package ru.mystamps.web.service;
1919

20+
import java.net.URI;
21+
import java.net.URISyntaxException;
2022
import java.util.Date;
2123
import java.util.List;
2224

@@ -63,12 +65,19 @@ public class SeriesImportServiceImpl implements SeriesImportService {
6365
@SuppressWarnings({ "PMD.NPathComplexity", "PMD.ModifiedCyclomaticComplexity" })
6466
public Integer addRequest(RequestImportDto dto, Integer userId) {
6567
Validate.isTrue(dto != null, "DTO must be non null");
68+
Validate.isTrue(dto.getUrl() != null, "URL must be non null");
6669
Validate.isTrue(userId != null, "Current user id must be non null");
6770

6871
ImportSeriesDbDto importRequest = new ImportSeriesDbDto();
69-
importRequest.setUrl(dto.getUrl());
7072
importRequest.setStatus(SeriesImportRequestStatus.UNPROCESSED);
7173

74+
try {
75+
String encodedUrl = new URI(dto.getUrl()).toASCIIString();
76+
importRequest.setUrl(encodedUrl);
77+
} catch (URISyntaxException ex) {
78+
throw new RuntimeException(ex); // NOPMD: AvoidThrowingRawExceptionTypes
79+
}
80+
7281
Date now = new Date();
7382
importRequest.setUpdatedAt(now);
7483
importRequest.setRequestedAt(now);

src/test/groovy/ru/mystamps/web/service/SeriesImportServiceImplTest.groovy

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,33 @@ class SeriesImportServiceImplTest extends Specification {
7575
thrown IllegalArgumentException
7676
}
7777

78+
def 'addRequest() should throw exception if url is null'() {
79+
given:
80+
form.setUrl(null)
81+
when:
82+
service.addRequest(form, Random.userId())
83+
then:
84+
thrown IllegalArgumentException
85+
}
86+
7887
def 'addRequest() should throw exception if user id is null'() {
7988
when:
8089
service.addRequest(form, null)
8190
then:
8291
thrown IllegalArgumentException
8392
}
8493

94+
def 'addRequest() should throw exception if url is incorrect'() {
95+
given:
96+
form.setUrl('http://example.org/текст c пробелами')
97+
when:
98+
service.addRequest(form, Random.userId())
99+
then:
100+
RuntimeException ex = thrown()
101+
and:
102+
ex?.cause?.class == URISyntaxException
103+
}
104+
85105
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
86106
def 'addRequest() should pass dto to dao and return its result'() {
87107
given:
@@ -105,6 +125,37 @@ class SeriesImportServiceImplTest extends Specification {
105125
result == expectedResult
106126
}
107127

128+
@SuppressWarnings(['ClosureAsLastMethodParameter', 'LineLength', 'UnnecessaryReturnKeyword'])
129+
def 'addRequest() should save url in the encoded form'() {
130+
given:
131+
String url = 'http://example.org/текст_на_русском'
132+
String expectedUrl = 'http://example.org/%D1%82%D0%B5%D0%BA%D1%81%D1%82_%D0%BD%D0%B0_%D1%80%D1%83%D1%81%D1%81%D0%BA%D0%BE%D0%BC'
133+
and:
134+
form.setUrl(url)
135+
when:
136+
service.addRequest(form, Random.userId())
137+
then:
138+
1 * seriesImportDao.add({ ImportSeriesDbDto request ->
139+
assert request?.url == expectedUrl
140+
return true
141+
}) >> Random.id()
142+
}
143+
144+
@SuppressWarnings(['ClosureAsLastMethodParameter', 'LineLength', 'UnnecessaryReturnKeyword'])
145+
def 'addRequest() should not encode url if it is already encoded'() {
146+
given:
147+
String expectedUrl = 'http://example.org/%D1%82%D0%B5%D0%BA%D1%81%D1%82_%D0%BD%D0%B0_%D1%80%D1%83%D1%81%D1%81%D0%BA%D0%BE%D0%BC'
148+
and:
149+
form.setUrl(expectedUrl)
150+
when:
151+
service.addRequest(form, Random.userId())
152+
then:
153+
1 * seriesImportDao.add({ ImportSeriesDbDto request ->
154+
assert request?.url == expectedUrl
155+
return true
156+
}) >> Random.id()
157+
}
158+
108159
//
109160
// Tests for addSeries()
110161
//

src/test/robotframework/series/import/request-logic.robot

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Import series from an external site (in English, use category, country and date
5555

5656
Import series from an external site (in Russian, use description locator)
5757
[Documentation] Verify import from a page in Russian and shared locator
58-
Input Text id=url http://localhost:8080/series/2?lang=ru
58+
Input Text id=url http://localhost:8080/series/2?lang=ru&str=тест
5959
Submit Form id=import-series-form
6060
${location}= Get Location
6161
Should Match Regexp ${location} /series/import/request/\\d+
@@ -66,7 +66,7 @@ Import series from an external site (in Russian, use description locator)
6666
${quantity}= Get Value id=quantity
6767
${imageUrl}= Get Value id=image-url
6868
${year}= Get Selected List Label id=year
69-
Element Text Should Be id=request-url http://localhost:8080/series/2?lang=ru
69+
Element Text Should Be id=request-url http://localhost:8080/series/2?lang=ru&str=%D1%82%D0%B5%D1%81%D1%82
7070
Element Text Should Be id=request-status ParsingSucceeded
7171
Should Be Equal ${category} Prehistoric animals
7272
Should Be Equal ${country} Italy

0 commit comments

Comments
 (0)