Skip to content

Commit fe20c33

Browse files
authored
normalize slash (#33)
1 parent 00162f2 commit fe20c33

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

client/client.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,12 @@ func (r *Request) Execute(method string, url string, body io.Reader, result inte
134134
}
135135

136136
func (r *Request) GetBase(path string) string {
137+
baseUrl := strings.TrimRight(r.BaseUrl, "/")
137138
if path == "" {
138-
return r.BaseUrl
139+
return baseUrl
139140
}
140-
return fmt.Sprintf("%s/%s", r.BaseUrl, path)
141+
path = strings.TrimLeft(path, "/")
142+
return fmt.Sprintf("%s/%s", baseUrl, path)
141143
}
142144

143145
func GetBody(body interface{}) (buf io.ReadWriter, err error) {

client/client_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package client
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestRequest_GetBase(t *testing.T) {
8+
type fields struct {
9+
baseUrl string
10+
}
11+
tests := []struct {
12+
name string
13+
fields fields
14+
path string
15+
want string
16+
}{
17+
{
18+
name: "Test base url ends with /, path starts with /",
19+
fields: fields{
20+
baseUrl: "https://api.example.com/",
21+
},
22+
path: "/v1/account/0x32Be343B94f860124dC4fEe278FDCBD38C102D88",
23+
want: "https://api.example.com/v1/account/0x32Be343B94f860124dC4fEe278FDCBD38C102D88",
24+
},
25+
{
26+
name: "Test only base url ends with /",
27+
fields: fields{
28+
baseUrl: "https://api.example.com/",
29+
},
30+
path: "v1/account/0x32Be343B94f860124dC4fEe278FDCBD38C102D88",
31+
want: "https://api.example.com/v1/account/0x32Be343B94f860124dC4fEe278FDCBD38C102D88",
32+
},
33+
{
34+
name: "Test only path starts with /",
35+
fields: fields{
36+
baseUrl: "https://api.example.com",
37+
},
38+
path: "/v1/account/0x32Be343B94f860124dC4fEe278FDCBD38C102D88",
39+
want: "https://api.example.com/v1/account/0x32Be343B94f860124dC4fEe278FDCBD38C102D88",
40+
},
41+
{
42+
name: "Test none /",
43+
fields: fields{
44+
baseUrl: "https://api.example.com",
45+
},
46+
path: "v1/account/0x32Be343B94f860124dC4fEe278FDCBD38C102D88",
47+
want: "https://api.example.com/v1/account/0x32Be343B94f860124dC4fEe278FDCBD38C102D88",
48+
},
49+
{
50+
name: "Test empty path",
51+
fields: fields{
52+
baseUrl: "https://api.example.com/",
53+
},
54+
path: "",
55+
want: "https://api.example.com",
56+
},
57+
}
58+
for _, tt := range tests {
59+
t.Run(tt.name, func(t *testing.T) {
60+
r := InitClient(tt.fields.baseUrl)
61+
if got := r.GetBase(tt.path); got != tt.want {
62+
t.Errorf("Request.GetBase() = %v, want %v", got, tt.want)
63+
}
64+
})
65+
}
66+
}

0 commit comments

Comments
 (0)