Skip to content

Commit

Permalink
Preserve *partially* parseable query strings too
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry committed Jul 22, 2024
1 parent d21b225 commit ecd166f
Show file tree
Hide file tree
Showing 37 changed files with 45 additions and 42 deletions.
15 changes: 9 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,16 @@ HTTPSnippet.prototype.prepare = function (request) {
// eslint-disable-next-line node/no-deprecated-api
request.uriObj = url.parse(request.url, true, true)

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

if (simpleQueryString) {
// merge all possible queryString values
request.queryObj = Object.assign(request.queryObj, request.uriObj.query)

// In some cases (unparseable query - preference to use raw in exporter) the queryString might
// be empty while the URL search is not. In that case, we prefer the URL search.
const hasQueryObj = Object.keys(request.queryObj).length > 0
if (hasQueryObj || !request.uriObj.search) {
// reset uriObj values for a clean url
request.uriObj.query = null
request.uriObj.search = null
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/output/c/libcurl/unparseable-query.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CURL *hnd = curl_easy_init();

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

CURLcode ret = curl_easy_perform(hnd);
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(require '[clj-http.client :as client])

(client/get "http://mockbin.com/har?&&&")
(client/get "http://mockbin.com/har?&&a=b&&")
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri("http://mockbin.com/har?&&&"),
RequestUri = new Uri("http://mockbin.com/har?&&a=b&&"),
};
using (var response = await client.SendAsync(request))
{
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/output/csharp/restsharp/unparseable-query.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
var client = new RestClient("http://mockbin.com/har?&&&");
var client = new RestClient("http://mockbin.com/har?&&a=b&&");
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
2 changes: 1 addition & 1 deletion test/fixtures/output/go/native/unparseable-query.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func main() {

url := "http://mockbin.com/har?&&&"
url := "http://mockbin.com/har?&&a=b&&"

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

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/output/http/1.1/unparseable-query
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
GET /har?&&& HTTP/1.1
GET /har?&&a=b&& HTTP/1.1
Host: mockbin.com
2 changes: 1 addition & 1 deletion test/fixtures/output/java/asynchttp/unparseable-query.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AsyncHttpClient client = new DefaultAsyncHttpClient();
client.prepare("GET", "http://mockbin.com/har?&&&")
client.prepare("GET", "http://mockbin.com/har?&&a=b&&")
.execute()
.toCompletableFuture()
.thenAccept(System.out::println)
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/output/java/nethttp/unparseable-query.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://mockbin.com/har?&&&"))
.uri(URI.create("http://mockbin.com/har?&&a=b&&"))
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/output/java/okhttp/unparseable-query.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()
.url("http://mockbin.com/har?&&&")
.url("http://mockbin.com/har?&&a=b&&")
.get()
.build();

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/output/java/unirest/unparseable-query.java
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
HttpResponse<String> response = Unirest.get("http://mockbin.com/har?&&&")
HttpResponse<String> response = Unirest.get("http://mockbin.com/har?&&a=b&&")
.asString();
2 changes: 1 addition & 1 deletion test/fixtures/output/javascript/axios/unparseable-query.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from "axios";

const options = {method: 'GET', url: 'http://mockbin.com/har?&&&'};
const options = {method: 'GET', url: 'http://mockbin.com/har?&&a=b&&'};

axios.request(options).then(function (response) {
console.log(response.data);
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/output/javascript/fetch/unparseable-query.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const options = {method: 'GET'};

fetch('http://mockbin.com/har?&&&', options)
fetch('http://mockbin.com/har?&&a=b&&', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const settings = {
"async": true,
"crossDomain": true,
"url": "http://mockbin.com/har?&&&",
"url": "http://mockbin.com/har?&&a=b&&",
"method": "GET",
"headers": {}
};
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/output/javascript/xhr/unparseable-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ xhr.addEventListener("readystatechange", function () {
}
});

xhr.open("GET", "http://mockbin.com/har?&&&");
xhr.open("GET", "http://mockbin.com/har?&&a=b&&");

xhr.send(data);
2 changes: 1 addition & 1 deletion test/fixtures/output/kotlin/okhttp/unparseable-query.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
val client = OkHttpClient()

val request = Request.Builder()
.url("http://mockbin.com/har?&&&")
.url("http://mockbin.com/har?&&a=b&&")
.get()
.build()

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/output/node/axios/unparseable-query.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var axios = require("axios").default;

var options = {method: 'GET', url: 'http://mockbin.com/har?&&&'};
var options = {method: 'GET', url: 'http://mockbin.com/har?&&a=b&&'};

axios.request(options).then(function (response) {
console.log(response.data);
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/output/node/fetch/unparseable-query.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fetch = require('node-fetch');

let url = 'http://mockbin.com/har?&&&';
let url = 'http://mockbin.com/har?&&a=b&&';

let options = {method: 'GET'};

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/output/node/native/unparseable-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const options = {
"method": "GET",
"hostname": "mockbin.com",
"port": null,
"path": "/har?&&&",
"path": "/har?&&a=b&&",
"headers": {}
};

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/output/node/request/unparseable-query.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const request = require('request');

const options = {method: 'GET', url: 'http://mockbin.com/har?&&&'};
const options = {method: 'GET', url: 'http://mockbin.com/har?&&a=b&&'};

request(options, function (error, response, body) {
if (error) throw new Error(error);
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/output/node/unirest/unparseable-query.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const unirest = require("unirest");

const req = unirest("GET", "http://mockbin.com/har?&&&");
const req = unirest("GET", "http://mockbin.com/har?&&a=b&&");

req.end(function (res) {
if (res.error) throw new Error(res.error);
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/output/objc/nsurlsession/unparseable-query.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#import <Foundation/Foundation.h>

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://mockbin.com/har?&&&"]
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://mockbin.com/har?&&a=b&&"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
[request setHTTPMethod:@"GET"];
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/output/ocaml/cohttp/unparseable-query.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ open Cohttp_lwt_unix
open Cohttp
open Lwt

let uri = Uri.of_string "http://mockbin.com/har?&&&" in
let uri = Uri.of_string "http://mockbin.com/har?&&a=b&&" in

Client.call `GET uri
>>= fun (res, body_stream) ->
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/output/php/curl/unparseable-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_URL => 'http://mockbin.com/har?&&&',
CURLOPT_URL => 'http://mockbin.com/har?&&a=b&&',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/output/php/http1/unparseable-query.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

$request = new HttpRequest();
$request->setUrl('http://mockbin.com/har?&&&');
$request->setUrl('http://mockbin.com/har?&&a=b&&');
$request->setMethod(HTTP_METH_GET);

try {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/output/php/http2/unparseable-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
$client = new http\Client;
$request = new http\Client\Request;

$request->setRequestUrl('http://mockbin.com/har?&&&');
$request->setRequestUrl('http://mockbin.com/har?&&a=b&&');
$request->setRequestMethod('GET');
$client->enqueue($request)->send();
$response = $client->getResponse();
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
$response = Invoke-RestMethod -Uri 'http://mockbin.com/har?&&&' -Method GET
$response = Invoke-RestMethod -Uri 'http://mockbin.com/har?&&a=b&&' -Method GET
Original file line number Diff line number Diff line change
@@ -1 +1 @@
$response = Invoke-WebRequest -Uri 'http://mockbin.com/har?&&&' -Method GET
$response = Invoke-WebRequest -Uri 'http://mockbin.com/har?&&a=b&&' -Method GET
2 changes: 1 addition & 1 deletion test/fixtures/output/python/python3/unparseable-query.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

conn = http.client.HTTPConnection("mockbin.com")

conn.request("GET", "/har?&&&")
conn.request("GET", "/har?&&a=b&&")

res = conn.getresponse()
data = res.read()
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/output/python/requests/unparseable-query.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import requests

url = "http://mockbin.com/har?&&&"
url = "http://mockbin.com/har?&&a=b&&"

response = requests.get(url)

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/output/r/httr/unparseable-query.r
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library(httr)

url <- "http://mockbin.com/har?&&&"
url <- "http://mockbin.com/har?&&a=b&&"

response <- VERB("GET", url, content_type("application/octet-stream"))

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/output/ruby/native/unparseable-query.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'uri'
require 'net/http'

url = URI("http://mockbin.com/har?&&&")
url = URI("http://mockbin.com/har?&&a=b&&")

http = Net::HTTP.new(url.host, url.port)

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/output/shell/curl/unparseable-query.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
curl --request GET \
--url 'http://mockbin.com/har?&&&'
--url 'http://mockbin.com/har?&&a=b&&'
2 changes: 1 addition & 1 deletion test/fixtures/output/shell/httpie/unparseable-query.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
http GET 'http://mockbin.com/har?&&&'
http GET 'http://mockbin.com/har?&&a=b&&'
2 changes: 1 addition & 1 deletion test/fixtures/output/shell/wget/unparseable-query.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
wget --quiet \
--method GET \
--output-document \
- 'http://mockbin.com/har?&&&'
- 'http://mockbin.com/har?&&a=b&&'
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

let request = NSMutableURLRequest(url: NSURL(string: "http://mockbin.com/har?&&&")! as URL,
let request = NSMutableURLRequest(url: NSURL(string: "http://mockbin.com/har?&&a=b&&")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
request.httpMethod = "GET"
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/requests/unparseable-query.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"method": "GET",
"url": "http://mockbin.com/har?&&&",
"url": "http://mockbin.com/har?&&a=b&&",
"httpVersion": "HTTP/1.1",
"queryString": []
}

0 comments on commit ecd166f

Please sign in to comment.