Skip to content

Commit ecd166f

Browse files
committed
Preserve *partially* parseable query strings too
1 parent d21b225 commit ecd166f

37 files changed

+45
-42
lines changed

src/index.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,16 @@ HTTPSnippet.prototype.prepare = function (request) {
216216
// eslint-disable-next-line node/no-deprecated-api
217217
request.uriObj = url.parse(request.url, true, true)
218218

219-
// merge all possible queryString values
220-
request.queryObj = Object.assign(request.queryObj, request.uriObj.query)
219+
// In some cases (unparseable/partially parseable query string) we want to fully preserve the
220+
// original string (as it may not follow qs conventions at all). We assume any scenario where
221+
// qs cannot reproduce the original value is this case.
222+
const simpleQueryString = !request.uriObj.search ||
223+
(qs.stringify(request.uriObj.query) === request.uriObj.search.slice(1))
224+
225+
if (simpleQueryString) {
226+
// merge all possible queryString values
227+
request.queryObj = Object.assign(request.queryObj, request.uriObj.query)
221228

222-
// In some cases (unparseable query - preference to use raw in exporter) the queryString might
223-
// be empty while the URL search is not. In that case, we prefer the URL search.
224-
const hasQueryObj = Object.keys(request.queryObj).length > 0
225-
if (hasQueryObj || !request.uriObj.search) {
226229
// reset uriObj values for a clean url
227230
request.uriObj.query = null
228231
request.uriObj.search = null
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CURL *hnd = curl_easy_init();
22

33
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "GET");
4-
curl_easy_setopt(hnd, CURLOPT_URL, "http://mockbin.com/har?&&&");
4+
curl_easy_setopt(hnd, CURLOPT_URL, "http://mockbin.com/har?&&a=b&&");
55

66
CURLcode ret = curl_easy_perform(hnd);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
(require '[clj-http.client :as client])
22

3-
(client/get "http://mockbin.com/har?&&&")
3+
(client/get "http://mockbin.com/har?&&a=b&&")

test/fixtures/output/csharp/httpclient/unparseable-query.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
var request = new HttpRequestMessage
33
{
44
Method = HttpMethod.Get,
5-
RequestUri = new Uri("http://mockbin.com/har?&&&"),
5+
RequestUri = new Uri("http://mockbin.com/har?&&a=b&&"),
66
};
77
using (var response = await client.SendAsync(request))
88
{
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
var client = new RestClient("http://mockbin.com/har?&&&");
1+
var client = new RestClient("http://mockbin.com/har?&&a=b&&");
22
var request = new RestRequest(Method.GET);
33
IRestResponse response = client.Execute(request);

test/fixtures/output/go/native/unparseable-query.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
func main() {
1010

11-
url := "http://mockbin.com/har?&&&"
11+
url := "http://mockbin.com/har?&&a=b&&"
1212

1313
req, _ := http.NewRequest("GET", url, nil)
1414

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
GET /har?&&& HTTP/1.1
1+
GET /har?&&a=b&& HTTP/1.1
22
Host: mockbin.com

test/fixtures/output/java/asynchttp/unparseable-query.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
AsyncHttpClient client = new DefaultAsyncHttpClient();
2-
client.prepare("GET", "http://mockbin.com/har?&&&")
2+
client.prepare("GET", "http://mockbin.com/har?&&a=b&&")
33
.execute()
44
.toCompletableFuture()
55
.thenAccept(System.out::println)

test/fixtures/output/java/nethttp/unparseable-query.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
HttpRequest request = HttpRequest.newBuilder()
2-
.uri(URI.create("http://mockbin.com/har?&&&"))
2+
.uri(URI.create("http://mockbin.com/har?&&a=b&&"))
33
.method("GET", HttpRequest.BodyPublishers.noBody())
44
.build();
55
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());

test/fixtures/output/java/okhttp/unparseable-query.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
OkHttpClient client = new OkHttpClient();
22

33
Request request = new Request.Builder()
4-
.url("http://mockbin.com/har?&&&")
4+
.url("http://mockbin.com/har?&&a=b&&")
55
.get()
66
.build();
77

0 commit comments

Comments
 (0)