1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
|
// Copyright 2015 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package httprequest_test
import (
"fmt"
"io/ioutil"
"net/http"
"time"
gc "gopkg.in/check.v1"
"gopkg.in/errgo.v1"
"gopkg.in/httprequest.v1"
)
type marshalSuite struct{}
var _ = gc.Suite(&marshalSuite{})
type embedded struct {
F1 string `json:"name"`
F2 int `json:"age"`
F3 *string `json:"address"`
}
var marshalTests = []struct {
about string
urlString string
method string
val interface{}
expectURLString string
expectBody *string
expectHeader http.Header
expectError string
}{{
about: "struct with simple fields",
urlString: "http://localhost:8081/:F01",
val: &struct {
F01 int `httprequest:",path"`
F02 string `httprequest:",form"`
F03 string `httprequest:",form,omitempty"`
F04 string `httprequest:",form,omitempty"`
F06 time.Time `httprequest:",form,omitempty"`
F07 time.Time `httprequest:",form,omitempty"`
F08 *time.Time `httprequest:",form,omitempty"`
F09 *time.Time `httprequest:",form,omitempty"`
F10 stringer `httprequest:",form,omitempty"`
F11 stringer `httprequest:",form,omitempty"`
F12 *stringer `httprequest:",form,omitempty"`
F13 *stringer `httprequest:",form,omitempty"`
F14 *stringer `httprequest:",form,omitempty"`
F15 time.Time `httprequest:",form"`
// Note that this gets omitted anyway because it's nil.
F16 *time.Time `httprequest:",form"`
}{
F01: 99,
F02: "some text",
F03: "",
F04: "something",
F07: time.Date(2001, 2, 3, 4, 5, 6, 0, time.UTC),
F09: func() *time.Time {
t := time.Date(2011, 2, 3, 4, 5, 6, 0, time.UTC)
return &t
}(),
F11: stringer(99),
F13: func() *stringer {
s := stringer(99)
return &s
}(),
F14: new(stringer),
},
expectURLString: "http://localhost:8081/99" +
"?F02=some+text" +
"&F04=something" +
"&F07=2001-02-03T04%3A05%3A06Z" +
"&F09=2011-02-03T04%3A05%3A06Z" +
"&F11=str99" +
"&F13=str99" +
"&F15=0001-01-01T00%3A00%3A00Z",
}, {
about: "struct with renamed fields",
urlString: "http://localhost:8081/:name",
val: &struct {
F1 string `httprequest:"name,path"`
F2 int `httprequest:"age,form"`
}{
F1: "some random user",
F2: 42,
},
expectURLString: "http://localhost:8081/some%20random%20user?age=42",
}, {
about: "fields without httprequest tags are ignored",
urlString: "http://localhost:8081/:name",
val: &struct {
F1 string `httprequest:"name,path"`
F2 int `httprequest:"age,form"`
F3 string
}{
F1: "some random user",
F2: 42,
F3: "some more random text",
},
expectURLString: "http://localhost:8081/some%20random%20user?age=42",
}, {
about: "pointer fields are correctly handled",
urlString: "http://localhost:8081/:name",
val: &struct {
F1 *string `httprequest:"name,path"`
F2 *string `httprequest:"age,form"`
F3 *string `httprequest:"address,form"`
}{
F1: newString("some random user"),
F2: newString("42"),
},
expectURLString: "http://localhost:8081/some%20random%20user?age=42",
}, {
about: "MarshalText called on TextMarshalers",
urlString: "http://localhost:8081/:param1/:param2",
val: &struct {
F1 testMarshaler `httprequest:"param1,path"`
F2 *testMarshaler `httprequest:"param2,path"`
F3 testMarshaler `httprequest:"param3,form"`
F4 *testMarshaler `httprequest:"param4,form"`
}{
F1: "test1",
F2: (*testMarshaler)(newString("test2")),
F3: "test3",
F4: (*testMarshaler)(newString("test4")),
},
expectURLString: "http://localhost:8081/test_test1/test_test2?param3=test_test3¶m4=test_test4",
}, {
about: "MarshalText not called on values that do not implement TextMarshaler",
urlString: "http://localhost:8081/user/:name/:surname",
val: &struct {
F1 notTextMarshaler `httprequest:"name,path"`
F2 *notTextMarshaler `httprequest:"surname,path"`
}{
F1: "name",
F2: (*notTextMarshaler)(newString("surname")),
},
expectURLString: "http://localhost:8081/user/name/surname",
}, {
about: "MarshalText returns an error",
urlString: "http://localhost:8081/user/:name/:surname",
val: &struct {
F1 testMarshaler `httprequest:"name,path"`
F2 *testMarshaler `httprequest:"surname,path"`
}{
F1: "",
F2: (*testMarshaler)(newString("surname")),
},
expectError: "cannot marshal field: empty string",
}, {
about: "[]string field form value",
urlString: "http://localhost:8081/user",
val: &struct {
F1 []string `httprequest:"users,form"`
}{
F1: []string{"user1", "user2", "user3"},
},
expectURLString: "http://localhost:8081/user?users=user1&users=user2&users=user3",
}, {
about: "nil []string field form value",
urlString: "http://localhost:8081/user",
val: &struct {
F1 *[]string `httprequest:"users,form"`
}{
F1: nil,
},
expectURLString: "http://localhost:8081/user",
}, {
about: "cannot marshal []string field to path",
urlString: "http://localhost:8081/:users",
val: &struct {
F1 []string `httprequest:"users,path"`
}{
F1: []string{"user1", "user2"},
},
expectError: `bad type \*struct { F1 \[\]string "httprequest:\\"users,path\\"" }: invalid target type \[\]string for path parameter`,
}, {
about: "[]string field fails to marshal to path",
urlString: "http://localhost:8081/user/:users",
val: &struct {
F1 []string `httprequest:"users,path"`
}{
F1: []string{"user1", "user2", "user3"},
},
expectError: "bad type .*: invalid target type.*",
}, {
about: "omitempty on body",
urlString: "http://localhost:8081/:users",
val: &struct {
Body string `httprequest:",body,omitempty"`
}{},
expectError: `bad type \*struct { Body string "httprequest:\\",body,omitempty\\"" }: bad tag "httprequest:\\",body,omitempty\\"" in field Body: can only use omitempty with form or header fields`,
}, {
about: "omitempty on path",
urlString: "http://localhost:8081/:Users",
val: &struct {
Users string `httprequest:",path,omitempty"`
}{},
expectError: `bad type \*struct { Users string "httprequest:\\",path,omitempty\\"" }: bad tag "httprequest:\\",path,omitempty\\"" in field Users: can only use omitempty with form or header fields`,
}, {
about: "more than one field with body tag",
urlString: "http://localhost:8081/user",
method: "POST",
val: &struct {
F1 string `httprequest:"user,body"`
F2 int `httprequest:"age,body"`
}{
F1: "test user",
F2: 42,
},
expectError: "bad type .*: more than one body field specified",
}, {
about: "required path parameter, but not specified",
urlString: "http://localhost:8081/u/:username",
method: "POST",
val: &struct {
F1 string `httprequest:"user,body"`
}{
F1: "test user",
},
expectError: `missing value for path parameter "username"`,
}, {
about: "marshal to body",
urlString: "http://localhost:8081/u",
method: "POST",
val: &struct {
F1 embedded `httprequest:"info,body"`
}{
F1: embedded{
F1: "test user",
F2: 42,
F3: newString("test address"),
},
},
expectBody: newString(`{"name":"test user","age":42,"address":"test address"}`),
}, {
about: "empty path wildcard",
urlString: "http://localhost:8081/u/:",
method: "POST",
val: &struct {
F1 string `httprequest:"user,body"`
}{
F1: "test user",
},
expectError: "empty path parameter",
}, {
about: "nil field to form",
urlString: "http://localhost:8081/u",
val: &struct {
F1 *string `httprequest:"user,form"`
}{},
expectURLString: "http://localhost:8081/u",
}, {
about: "nil field to path",
urlString: "http://localhost:8081/u",
val: &struct {
F1 *string `httprequest:"user,path"`
}{},
expectURLString: "http://localhost:8081/u",
}, {
about: "marshal to body of a GET request",
urlString: "http://localhost:8081/u",
val: &struct {
F1 string `httprequest:",body"`
}{
F1: "hello test",
},
// Providing a body to a GET request is unusual but
// some people do it anyway.
expectBody: newString(`"hello test"`),
}, {
about: "marshal to nil value to body",
urlString: "http://localhost:8081/u",
val: &struct {
F1 *string `httprequest:",body"`
}{
F1: nil,
},
expectBody: newString(""),
}, {
about: "nil TextMarshaler",
urlString: "http://localhost:8081/u",
val: &struct {
F1 *testMarshaler `httprequest:"surname,form"`
}{
F1: (*testMarshaler)(nil),
},
expectURLString: "http://localhost:8081/u",
}, {
about: "marshal nil with Sprint",
urlString: "http://localhost:8081/u",
val: &struct {
F1 *int `httprequest:"surname,form"`
}{
F1: (*int)(nil),
},
expectURLString: "http://localhost:8081/u",
}, {
about: "marshal to path with * placeholder",
urlString: "http://localhost:8081/u/*name",
val: &struct {
F1 string `httprequest:"name,path"`
}{
F1: "/test",
},
expectURLString: "http://localhost:8081/u/test",
}, {
about: "marshal to path with * placeholder, but the marshaled value does not start with /",
urlString: "http://localhost:8081/u/*name",
val: &struct {
F1 string `httprequest:"name,path"`
}{
F1: "test",
},
expectError: `value \"test\" for path parameter \"\*name\" does not start with required /`,
}, {
about: "* placeholder allowed only at the end",
urlString: "http://localhost:8081/u/*name/document",
val: &struct {
F1 string `httprequest:"name,path"`
}{
F1: "test",
},
expectError: "star path parameter is not at end of path",
}, {
about: "unparsable base url string",
urlString: "%%",
val: &struct {
F1 string `httprequest:"name,form"`
}{
F1: "test",
},
expectError: `parse %%: invalid URL escape \"%%\"`,
}, {
about: "value cannot be marshaled to json",
urlString: "http://localhost",
method: "POST",
val: &struct {
F1 failJSONMarshaler `httprequest:"field,body"`
}{
F1: "test",
},
expectError: `cannot marshal field: cannot marshal request body: json: error calling MarshalJSON for type \*httprequest_test.failJSONMarshaler: marshal error`,
}, {
about: "url with query parameters",
urlString: "http://localhost?a=b",
method: "POST",
val: &struct {
F1 failJSONMarshaler `httprequest:"f1,form"`
}{
F1: "test",
},
expectURLString: "http://localhost?a=b&f1=test",
}, {
about: "url with query parameters no form",
urlString: "http://localhost?a=b",
method: "POST",
val: &struct{}{},
expectURLString: "http://localhost?a=b",
}, {
about: "struct with headers",
urlString: "http://localhost:8081/",
val: &struct {
F01 string `httprequest:",header"`
F02 int `httprequest:",header"`
F03 bool `httprequest:",header"`
F04 string `httprequest:",header,omitempty"`
F05 string `httprequest:",header,omitempty"`
F06 time.Time `httprequest:",header,omitempty"`
F07 time.Time `httprequest:",header,omitempty"`
F08 *time.Time `httprequest:",header,omitempty"`
F09 *time.Time `httprequest:",header,omitempty"`
F10 stringer `httprequest:",header,omitempty"`
F11 stringer `httprequest:",header,omitempty"`
F12 *stringer `httprequest:",header,omitempty"`
F13 *stringer `httprequest:",header,omitempty"`
F14 *stringer `httprequest:",header,omitempty"`
F15 time.Time `httprequest:",header"`
// Note that this gets omitted anyway because it's nil.
F16 *time.Time `httprequest:",header"`
}{
F01: "some text",
F02: 99,
F03: true,
F04: "",
F05: "something",
F07: time.Date(2001, 2, 3, 4, 5, 6, 0, time.UTC),
F09: func() *time.Time {
t := time.Date(2011, 2, 3, 4, 5, 6, 0, time.UTC)
return &t
}(),
F11: stringer(99),
F13: func() *stringer {
s := stringer(99)
return &s
}(),
F14: new(stringer),
},
expectURLString: "http://localhost:8081/",
expectHeader: http.Header{
"F01": {"some text"},
"F02": {"99"},
"F03": {"true"},
"F05": {"something"},
"F07": {"2001-02-03T04:05:06Z"},
"F09": {"2011-02-03T04:05:06Z"},
"F11": {"str99"},
"F13": {"str99"},
"F15": {"0001-01-01T00:00:00Z"},
},
}, {
about: "struct with header slice",
urlString: "http://localhost:8081/:F1",
val: &struct {
F1 int `httprequest:",path"`
F2 string `httprequest:",form"`
F3 []string `httprequest:",header"`
}{
F1: 99,
F2: "some text",
F3: []string{"A", "B", "C"},
},
expectURLString: "http://localhost:8081/99?F2=some+text",
expectHeader: http.Header{"F3": []string{"A", "B", "C"}},
}, {
about: "SetHeader called after marshaling",
urlString: "http://localhost:8081/",
val: &httprequest.CustomHeader{
Body: &struct {
F1 string `httprequest:",header"`
F2 int `httprequest:",header"`
F3 bool `httprequest:",header"`
}{
F1: "some text",
F2: 99,
F3: false,
},
SetHeaderFunc: func(h http.Header) {
h.Set("F2", "some other text")
},
},
expectURLString: "http://localhost:8081/",
expectHeader: http.Header{
"F1": {"some text"},
"F2": {"some other text"},
"F3": {"false"},
},
}}
func getStruct() interface{} {
return &struct {
F1 string
}{
F1: "hello",
}
}
func (*marshalSuite) TestMarshal(c *gc.C) {
for i, test := range marshalTests {
c.Logf("%d: %s", i, test.about)
method := "GET"
if test.method != "" {
method = test.method
}
req, err := httprequest.Marshal(test.urlString, method, test.val)
if test.expectError != "" {
c.Assert(err, gc.ErrorMatches, test.expectError)
continue
}
c.Assert(err, gc.IsNil)
if test.expectURLString != "" {
c.Assert(req.URL.String(), gc.DeepEquals, test.expectURLString)
}
if test.expectBody != nil {
data, err := ioutil.ReadAll(req.Body)
c.Assert(err, gc.IsNil)
if *test.expectBody != "" {
c.Assert(req.Header.Get("Content-Type"), gc.Equals, "application/json")
}
c.Assert(string(data), gc.DeepEquals, *test.expectBody)
}
for k, v := range test.expectHeader {
c.Assert(req.Header[k], gc.DeepEquals, v, gc.Commentf("key %q", k))
}
}
}
type testMarshaler string
func (t *testMarshaler) MarshalText() ([]byte, error) {
if len(*t) == 0 {
return nil, errgo.New("empty string")
}
return []byte("test_" + *t), nil
}
type notTextMarshaler string
// MarshalText does *not* implement encoding.TextMarshaler
func (t *notTextMarshaler) MarshalText() {
panic("unexpected call")
}
type failJSONMarshaler string
func (*failJSONMarshaler) MarshalJSON() ([]byte, error) {
return nil, errgo.New("marshal error")
}
type stringer int
func (s stringer) String() string {
return fmt.Sprintf("str%d", int(s))
}
|