@@ -26,6 +26,7 @@ import (
2626 "net"
2727 "net/http"
2828 "net/http/httptest"
29+ "net/url"
2930 "os"
3031 "path/filepath"
3132 "reflect"
@@ -36,6 +37,7 @@ import (
3637 "testing"
3738 "time"
3839
40+ "github.com/stretchr/testify/require"
3941 yaml "gopkg.in/yaml.v2"
4042)
4143
@@ -1276,3 +1278,40 @@ endpoint_params:
12761278 t .Fatalf ("Expected authorization header to be 'Bearer 12345', got '%s'" , authorization )
12771279 }
12781280}
1281+
1282+ func TestMarshalURL (t * testing.T ) {
1283+ urlp , err := url .Parse ("http://example.com/" )
1284+ if err != nil {
1285+ t .Fatal (err )
1286+ }
1287+ u := & URL {urlp }
1288+
1289+ c , err := json .Marshal (u )
1290+ if err != nil {
1291+ t .Fatal (err )
1292+ }
1293+ require .Equal (t , "\" http://example.com/\" " , string (c ), "URL not properly marshaled in JSON." )
1294+
1295+ c , err = yaml .Marshal (u )
1296+ if err != nil {
1297+ t .Fatal (err )
1298+ }
1299+ require .Equal (t , "http://example.com/\n " , string (c ), "URL not properly marshaled in YAML." )
1300+ }
1301+
1302+ func TestUnmarshalURL (t * testing.T ) {
1303+ b := []byte (`"http://example.com/a b"` )
1304+ var u URL
1305+
1306+ err := json .Unmarshal (b , & u )
1307+ if err != nil {
1308+ t .Fatal (err )
1309+ }
1310+ require .Equal (t , "http://example.com/a%20b" , u .String (), "URL not properly unmarshaled in JSON." )
1311+
1312+ err = yaml .Unmarshal (b , & u )
1313+ if err != nil {
1314+ t .Fatal (err )
1315+ }
1316+ require .Equal (t , "http://example.com/a%20b" , u .String (), "URL not properly unmarshaled in YAML." )
1317+ }
0 commit comments