Skip to content

Commit f1b2251

Browse files
committed
refactor: query and content-type
1 parent 9ba9fae commit f1b2251

File tree

9 files changed

+115
-34
lines changed

9 files changed

+115
-34
lines changed

src/targets/java/restclient.js

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,39 @@
1212

1313
const CodeBuilder = require('../../helpers/code-builder')
1414

15+
// Based off org.springframework.http.HttpMethod
1516
const standardMethods = ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'TRACE']
1617

18+
// Based off org.springframework.http.MediaType
19+
const standardMediaTypes = {
20+
'application/atom+xml': 'APPLICATION_ATOM_XML',
21+
'application/cbor': 'APPLICATION_CBOR',
22+
'application/x-www-form-urlencoded': 'APPLICATION_FORM_URLENCODED',
23+
'application/graphql-response+json': 'APPLICATION_GRAPHQL_RESPONSE',
24+
'application/json': 'APPLICATION_JSON',
25+
'application/x-ndjson': 'APPLICATION_NDJSON',
26+
'application/octet-stream': 'APPLICATION_OCTET_STREAM',
27+
'application/pdf': 'APPLICATION_PDF',
28+
'application/problem+json': 'APPLICATION_PROBLEM_JSON',
29+
'application/problem+xml': 'APPLICATION_PROBLEM_XML',
30+
'application/x-protobuf': 'APPLICATION_PROTOBUF',
31+
'application/rss+xml': 'APPLICATION_RSS_XML',
32+
'application/xhtml+xml': 'APPLICATION_XHTML_XML',
33+
'application/xml': 'APPLICATION_XML',
34+
'application/yaml': 'APPLICATION_YAML',
35+
'image/gif': 'IMAGE_GIF',
36+
'image/jpeg': 'IMAGE_JPEG',
37+
'image/png': 'IMAGE_PNG',
38+
'multipart/form-data': 'MULTIPART_FORM_DATA',
39+
'multipart/mixed': 'MULTIPART_MIXED',
40+
'multipart/related': 'MULTIPART_RELATED',
41+
'text/event-stream': 'TEXT_EVENT_STREAM',
42+
'text/html': 'TEXT_HTML',
43+
'text/markdown': 'TEXT_MARKDOWN',
44+
'text/plain': 'TEXT_PLAIN',
45+
'text/xml': 'TEXT_XML'
46+
}
47+
1748
module.exports = function (source, options) {
1849
const opts = Object.assign({
1950
indent: ' '
@@ -32,39 +63,48 @@ module.exports = function (source, options) {
3263
code.push(1, '.method(HttpMethod.valueOf("%s"))', source.method.toUpperCase())
3364
}
3465

35-
code.push(1, '.uri("%s")', source.fullUrl)
66+
if (Object.keys(source.queryObj).length) {
67+
code.push(1, '.uri("%s", uriBuilder -> {', source.url)
68+
Object.keys(source.queryObj).forEach(function (key) {
69+
const value = source.queryObj[key]
70+
if (Array.isArray(value)) {
71+
value.forEach(function (val) {
72+
code.push(2, 'uriBuilder.queryParam("%qd", "%qd");', key, val)
73+
})
74+
} else {
75+
code.push(2, 'uriBuilder.queryParam("%qd", "%qd");', key, value)
76+
}
77+
})
78+
code.push(2, 'return uriBuilder.build();')
79+
code.push(1, '})')
80+
} else {
81+
code.push(1, '.uri("%s")', source.url)
82+
}
3683

3784
if (source.cookies && source.cookies.length) {
3885
source.cookies.forEach(function (cookie) {
39-
code.push(1, '.cookie("%s", "%s")', cookie.name, cookie.value)
86+
code.push(1, '.cookie("%qd", "%qd")', cookie.name, cookie.value)
4087
})
4188
}
4289

43-
const headers = Object.keys(source.allHeaders).filter(function (key) {
44-
return key.toLowerCase() !== 'cookie'
45-
})
90+
const headers = Object.keys(source.headersObj)
4691
if (headers.length) {
4792
headers.forEach(function (key) {
48-
code.push(1, '.header("%s", "%qd")', key, source.allHeaders[key])
93+
code.push(1, '.header("%s", "%qd")', key, source.headersObj[key])
4994
})
5095
}
5196

5297
if (source.postData && source.postData.text) {
53-
if (source.postData.mimeType === 'application/json') {
54-
code.push(1, '.contentType(MediaType.APPLICATION_JSON)')
55-
code.push(1, '.body(%s)', JSON.stringify(source.postData.text))
56-
} else if (source.postData.mimeType === 'application/x-www-form-urlencoded') {
57-
code.push(1, '.contentType(MediaType.APPLICATION_FORM_URLENCODED)')
58-
code.push(1, '.body(%s)', JSON.stringify(source.postData.text))
59-
} else if (source.postData.mimeType && source.postData.mimeType.startsWith('multipart/form-data')) {
60-
code.push(1, '.contentType(MediaType.parseMediaType("multipart/form-data"))')
61-
code.push(1, '.body(%s)', JSON.stringify(source.postData.text))
62-
} else {
63-
if (source.postData.mimeType) {
98+
if (source.postData.mimeType) {
99+
const mappedEnumConst = standardMediaTypes[source.postData.mimeType]
100+
if (mappedEnumConst) {
101+
code.push(1, '.contentType(MediaType.%s)', mappedEnumConst)
102+
} else {
64103
code.push(1, '.contentType(MediaType.parseMediaType("%s"))', source.postData.mimeType)
65104
}
66-
code.push(1, '.body(%s)', JSON.stringify(source.postData.text))
67105
}
106+
107+
code.push(1, '.body(%s)', JSON.stringify(source.postData.text))
68108
}
69109

70110
code.push(1, '.retrieve()')

test/fixtures/output/java/restclient/full.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
ResponseEntity<String> response = restClient
44
.method(HttpMethod.POST)
5-
.uri("http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value")
5+
.uri("http://mockbin.com/har", uriBuilder -> {
6+
uriBuilder.queryParam("foo", "bar");
7+
uriBuilder.queryParam("foo", "baz");
8+
uriBuilder.queryParam("baz", "abc");
9+
uriBuilder.queryParam("key", "value");
10+
return uriBuilder.build();
11+
})
612
.cookie("foo", "bar")
713
.cookie("bar", "baz")
814
.header("accept", "application/json")

test/fixtures/output/java/restclient/malicious.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,31 @@
22

33
ResponseEntity<String> response = restClient
44
.method(HttpMethod.POST)
5-
.uri("http://example.test/%27%22%60$(%(%%7B%7B%7B/0%s//?'=squote-key-test&squote-value-test='&%22=dquote-key-test&dquote-value-test=%22&%60=backtick-key-test&backtick-value-test=%60&%24(=dollar-parenthesis-key-test&dollar-parenthesis-value-test=%24(&%23%7B=hash-brace-key-test&hash-brace-value-test=%23%7B&%25(=percent-parenthesis-key-test&percent-parenthesis-value-test=%25(&%25%7B=percent-brace-key-test&percent-brace-value-test=%25%7B&%7B%7B=double-brace-key-test&double-brace-value-test=%7B%7B&%5C0=null-key-test&null-value-test=%5C0&%25s=string-fmt-key-test&string-fmt-value-test=%25s&%5C=slash-key-test&slash-value-test=%5C")
5+
.uri("http://example.test/%27%22%60$(%(%%7B%7B%7B/0%s//", uriBuilder -> {
6+
uriBuilder.queryParam("'", "squote-key-test");
7+
uriBuilder.queryParam("squote-value-test", "'");
8+
uriBuilder.queryParam("\"", "dquote-key-test");
9+
uriBuilder.queryParam("dquote-value-test", "\"");
10+
uriBuilder.queryParam("`", "backtick-key-test");
11+
uriBuilder.queryParam("backtick-value-test", "`");
12+
uriBuilder.queryParam("$(", "dollar-parenthesis-key-test");
13+
uriBuilder.queryParam("dollar-parenthesis-value-test", "$(");
14+
uriBuilder.queryParam("#{", "hash-brace-key-test");
15+
uriBuilder.queryParam("hash-brace-value-test", "#{");
16+
uriBuilder.queryParam("%(", "percent-parenthesis-key-test");
17+
uriBuilder.queryParam("percent-parenthesis-value-test", "%(");
18+
uriBuilder.queryParam("%{", "percent-brace-key-test");
19+
uriBuilder.queryParam("percent-brace-value-test", "%{");
20+
uriBuilder.queryParam("{{", "double-brace-key-test");
21+
uriBuilder.queryParam("double-brace-value-test", "{{");
22+
uriBuilder.queryParam("\\0", "null-key-test");
23+
uriBuilder.queryParam("null-value-test", "\\0");
24+
uriBuilder.queryParam("%s", "string-fmt-key-test");
25+
uriBuilder.queryParam("string-fmt-value-test", "%s");
26+
uriBuilder.queryParam("\\", "slash-key-test");
27+
uriBuilder.queryParam("slash-value-test", "\\");
28+
return uriBuilder.build();
29+
})
630
.header("'", "squote-key-test")
731
.header("squote-value-test", "'")
832
.header("dquote-value-test", "\"")
@@ -19,7 +43,7 @@
1943
.header("null-value-test", "\\0")
2044
.header("string-fmt-value-test", "%s")
2145
.header("slash-value-test", "\\")
22-
.contentType(MediaType.parseMediaType("text/plain"))
46+
.contentType(MediaType.TEXT_PLAIN)
2347
.body("' \" ` $( #{ %( %{ {{ \\0 %s \\")
2448
.retrieve()
25-
.toEntity(String.class);
49+
.toEntity(String.class);

test/fixtures/output/java/restclient/multipart-data.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
.method(HttpMethod.POST)
55
.uri("http://mockbin.com/har")
66
.header("content-type", "multipart/form-data; boundary=---011000010111000001101001")
7-
.contentType(MediaType.parseMediaType("multipart/form-data"))
7+
.contentType(MediaType.MULTIPART_FORM_DATA)
88
.body("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001--\r\n")
99
.retrieve()
10-
.toEntity(String.class);
10+
.toEntity(String.class);

test/fixtures/output/java/restclient/multipart-file.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
.method(HttpMethod.POST)
55
.uri("http://mockbin.com/har")
66
.header("content-type", "multipart/form-data; boundary=---011000010111000001101001")
7-
.contentType(MediaType.parseMediaType("multipart/form-data"))
7+
.contentType(MediaType.MULTIPART_FORM_DATA)
88
.body("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\n\r\n-----011000010111000001101001--\r\n")
99
.retrieve()
10-
.toEntity(String.class);
10+
.toEntity(String.class);

test/fixtures/output/java/restclient/multipart-form-data.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
.method(HttpMethod.POST)
55
.uri("http://mockbin.com/har")
66
.header("Content-Type", "multipart/form-data; boundary=---011000010111000001101001")
7-
.contentType(MediaType.parseMediaType("multipart/form-data"))
7+
.contentType(MediaType.MULTIPART_FORM_DATA)
88
.body("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n-----011000010111000001101001--\r\n")
99
.retrieve()
10-
.toEntity(String.class);
10+
.toEntity(String.class);

test/fixtures/output/java/restclient/nested.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
ResponseEntity<String> response = restClient
44
.method(HttpMethod.GET)
5-
.uri("http://mockbin.com/har?foo%5Bbar%5D=baz%2Czap&fiz=buz&key=value")
5+
.uri("http://mockbin.com/har", uriBuilder -> {
6+
uriBuilder.queryParam("foo[bar]", "baz,zap");
7+
uriBuilder.queryParam("fiz", "buz");
8+
uriBuilder.queryParam("key", "value");
9+
return uriBuilder.build();
10+
})
611
.retrieve()
7-
.toEntity(String.class);
12+
.toEntity(String.class);

test/fixtures/output/java/restclient/query.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
ResponseEntity<String> response = restClient
44
.method(HttpMethod.GET)
5-
.uri("http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value")
5+
.uri("http://mockbin.com/har", uriBuilder -> {
6+
uriBuilder.queryParam("foo", "bar");
7+
uriBuilder.queryParam("foo", "baz");
8+
uriBuilder.queryParam("baz", "abc");
9+
uriBuilder.queryParam("key", "value");
10+
return uriBuilder.build();
11+
})
612
.retrieve()
7-
.toEntity(String.class);
13+
.toEntity(String.class);

test/fixtures/output/java/restclient/text-plain.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
.method(HttpMethod.POST)
55
.uri("http://mockbin.com/har")
66
.header("content-type", "text/plain")
7-
.contentType(MediaType.parseMediaType("text/plain"))
7+
.contentType(MediaType.TEXT_PLAIN)
88
.body("Hello World")
99
.retrieve()
10-
.toEntity(String.class);
10+
.toEntity(String.class);

0 commit comments

Comments
 (0)