| 10 | ) |
| 11 | |
| 12 | func TestBuildURL(t *testing.T) { |
| 13 | var preparedURL *url.URL |
| 14 | |
| 15 | preparedURL = buildURL("https://postman-echo.com", "/get", nil) |
| 16 | if !assert.Equal(t, preparedURL.String(), "https://postman-echo.com/get/") { |
| 17 | t.Fatal() |
| 18 | } |
| 19 | preparedURL = buildURL("https://postman-echo.com", "get", nil) |
| 20 | if !assert.Equal(t, preparedURL.String(), "https://postman-echo.com/get/") { |
| 21 | t.Fatal() |
| 22 | } |
| 23 | preparedURL = buildURL("https://postman-echo.com/", "/get", nil) |
| 24 | if !assert.Equal(t, preparedURL.String(), "https://postman-echo.com/get/") { |
| 25 | t.Fatal() |
| 26 | } |
| 27 | |
| 28 | preparedURL = buildURL("https://postman-echo.com/abc/", "/get?a=1&b=2", nil) |
| 29 | if !assert.Equal(t, preparedURL.String(), "https://postman-echo.com/abc/get?a=1&b=2") { |
| 30 | t.Fatal() |
| 31 | } |
| 32 | preparedURL = buildURL("https://postman-echo.com/abc", "get?a=1&b=2", nil) |
| 33 | if !assert.Equal(t, preparedURL.String(), "https://postman-echo.com/abc/get?a=1&b=2") { |
| 34 | t.Fatal() |
| 35 | } |
| 36 | |
| 37 | // omit query string in base url |
| 38 | preparedURL = buildURL("https://postman-echo.com/abc?x=6&y=9", "/get?a=1&b=2", nil) |
| 39 | if !assert.Equal(t, preparedURL.String(), "https://postman-echo.com/abc/get?a=1&b=2") { |
| 40 | t.Fatal() |
| 41 | } |
| 42 | |
| 43 | preparedURL = buildURL("", "https://postman-echo.com/get", nil) |
| 44 | if !assert.Equal(t, preparedURL.String(), "https://postman-echo.com/get/") { |
| 45 | t.Fatal() |
| 46 | } |
| 47 | |
| 48 | // notice: step request url > config base url |
| 49 | preparedURL = buildURL("https://postman-echo.com", "https://httpbin.org/get", nil) |
| 50 | if !assert.Equal(t, preparedURL.String(), "https://httpbin.org/get/") { |
| 51 | t.Fatal() |
| 52 | } |
| 53 | |
| 54 | // websocket url |
| 55 | preparedURL = buildURL("wss://ws.postman-echo.com/raw", "", nil) |
| 56 | if !assert.Equal(t, preparedURL.String(), "wss://ws.postman-echo.com/raw/") { |
| 57 | t.Fatal() |
| 58 | } |
| 59 | |
| 60 | preparedURL = buildURL("wss://ws.postman-echo.com", "/raw", nil) |
| 61 | if !assert.Equal(t, preparedURL.String(), "wss://ws.postman-echo.com/raw/") { |
| 62 | t.Fatal() |
| 63 | } |
| 64 | |
| 65 | preparedURL = buildURL("wss://ws.postman-echo.com/raw", "ws://echo.websocket.events", nil) |
| 66 | if !assert.Equal(t, preparedURL.String(), "ws://echo.websocket.events/") { |
| 67 | t.Fatal() |
| 68 | } |
| 69 | |