Skip to content

Commit

Permalink
fix: caused by hard suffixes bug httprunner#1684
Browse files Browse the repository at this point in the history
  • Loading branch information
xingheyang-mac committed Nov 5, 2023
1 parent d3303a2 commit 7ce3167
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
5 changes: 3 additions & 2 deletions hrp/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ func buildURL(baseURL, stepURL string, queryParams url.Values) (fullUrl *url.URL
}
}

// ensure path suffix '/' exists
if uStep.RawQuery == "" {
// Splice suffixes as needed "/"
isSuffix := strings.HasSuffix(stepURL, "/") || (strings.HasSuffix(baseURL, "/") && stepURL == "")
if uStep.RawQuery == "" && isSuffix {
uStep.Path = strings.TrimRight(uStep.Path, "/") + "/"
}

Expand Down
62 changes: 56 additions & 6 deletions hrp/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,27 @@ func TestBuildURL(t *testing.T) {
var preparedURL *url.URL

preparedURL = buildURL("https://postman-echo.com", "/get", nil)
if !assert.Equal(t, preparedURL.String(), "https://postman-echo.com/get/") {
if !assert.Equal(t, preparedURL.String(), "https://postman-echo.com/get") {
t.Fatal()
}
preparedURL = buildURL("https://postman-echo.com", "get", nil)
if !assert.Equal(t, preparedURL.String(), "https://postman-echo.com/get/") {
if !assert.Equal(t, preparedURL.String(), "https://postman-echo.com/get") {
t.Fatal()
}
preparedURL = buildURL("https://postman-echo.com/", "/get", nil)
if !assert.Equal(t, preparedURL.String(), "https://postman-echo.com/get") {
t.Fatal()
}

preparedURL = buildURL("https://postman-echo.com", "/get/", nil)
if !assert.Equal(t, preparedURL.String(), "https://postman-echo.com/get/") {
t.Fatal()
}
preparedURL = buildURL("https://postman-echo.com", "get/", nil)
if !assert.Equal(t, preparedURL.String(), "https://postman-echo.com/get/") {
t.Fatal()
}
preparedURL = buildURL("https://postman-echo.com/", "/get/", nil)
if !assert.Equal(t, preparedURL.String(), "https://postman-echo.com/get/") {
t.Fatal()
}
Expand All @@ -41,32 +54,69 @@ func TestBuildURL(t *testing.T) {
}

preparedURL = buildURL("", "https://postman-echo.com/get", nil)
if !assert.Equal(t, preparedURL.String(), "https://postman-echo.com/get/") {
if !assert.Equal(t, preparedURL.String(), "https://postman-echo.com/get") {
t.Fatal()
}

// notice: step request url > config base url
preparedURL = buildURL("https://postman-echo.com", "https://httpbin.org/get", nil)
if !assert.Equal(t, preparedURL.String(), "https://httpbin.org/get/") {
if !assert.Equal(t, preparedURL.String(), "https://httpbin.org/get") {
t.Fatal()
}

// websocket url
preparedURL = buildURL("wss://ws.postman-echo.com/raw", "", nil)
if !assert.Equal(t, preparedURL.String(), "wss://ws.postman-echo.com/raw/") {
if !assert.Equal(t, preparedURL.String(), "wss://ws.postman-echo.com/raw") {
t.Fatal()
}

preparedURL = buildURL("wss://ws.postman-echo.com", "/raw", nil)
if !assert.Equal(t, preparedURL.String(), "wss://ws.postman-echo.com/raw/") {
if !assert.Equal(t, preparedURL.String(), "wss://ws.postman-echo.com/raw") {
t.Fatal()
}

preparedURL = buildURL("wss://ws.postman-echo.com/raw", "ws://echo.websocket.events", nil)
if !assert.Equal(t, preparedURL.String(), "ws://echo.websocket.events") {
t.Fatal()
}

preparedURL = buildURL("", "https://postman-echo.com/get/", nil)
if !assert.Equal(t, preparedURL.String(), "https://postman-echo.com/get/") {
t.Fatal()
}

// notice: step request url > config base url
preparedURL = buildURL("https://postman-echo.com", "https://httpbin.org/get/", nil)
if !assert.Equal(t, preparedURL.String(), "https://httpbin.org/get/") {
t.Fatal()
}

// websocket url
preparedURL = buildURL("wss://ws.postman-echo.com/raw/", "", nil)
if !assert.Equal(t, preparedURL.String(), "wss://ws.postman-echo.com/raw/") {
t.Fatal()
}

preparedURL = buildURL("wss://ws.postman-echo.com", "/raw/", nil)
if !assert.Equal(t, preparedURL.String(), "wss://ws.postman-echo.com/raw/") {
t.Fatal()
}

preparedURL = buildURL("wss://ws.postman-echo.com/raw/", "ws://echo.websocket.events/", nil)
if !assert.Equal(t, preparedURL.String(), "ws://echo.websocket.events/") {
t.Fatal()
}

preparedURL = buildURL("wss://ws.postman-echo.com/raw", "ws://echo.websocket.events/", nil)
if !assert.Equal(t, preparedURL.String(), "ws://echo.websocket.events/") {
t.Fatal()
}

preparedURL = buildURL("wss://ws.postman-echo.com/raw/", "ws://echo.websocket.events", nil)
if !assert.Equal(t, preparedURL.String(), "ws://echo.websocket.events") {
t.Fatal()
}

queryParams := url.Values{}
queryParams.Add("c", "3")
queryParams.Add("d", "4")
Expand Down

0 comments on commit 7ce3167

Please sign in to comment.