File: preparer_test.go

package info (click to toggle)
golang-github-azure-go-autorest 14.2.0%2Bgit20220726.711dde1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,320 kB
  • sloc: makefile: 15
file content (892 lines) | stat: -rw-r--r-- 25,662 bytes parent folder | download
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
package autorest

// Copyright 2017 Microsoft Corporation
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.

import (
	"context"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"reflect"
	"strconv"
	"strings"
	"testing"

	"github.com/Azure/go-autorest/autorest/mocks"
)

// PrepareDecorators wrap and invoke a Preparer. Most often, the decorator invokes the passed
// Preparer and decorates the response.
func ExamplePrepareDecorator() {
	path := "a/b/c/"
	pd := func() PrepareDecorator {
		return func(p Preparer) Preparer {
			return PreparerFunc(func(r *http.Request) (*http.Request, error) {
				r, err := p.Prepare(r)
				if err == nil {
					if r.URL == nil {
						return r, fmt.Errorf("ERROR: URL is not set")
					}
					r.URL.Path += path
				}
				return r, err
			})
		}
	}

	r, _ := Prepare(&http.Request{},
		WithBaseURL("https://microsoft.com/"),
		pd())

	fmt.Printf("Path is %s\n", r.URL)
	// Output: Path is https://microsoft.com/a/b/c/
}

// PrepareDecorators may also modify and then invoke the Preparer.
func ExamplePrepareDecorator_pre() {
	pd := func() PrepareDecorator {
		return func(p Preparer) Preparer {
			return PreparerFunc(func(r *http.Request) (*http.Request, error) {
				r.Header.Add(http.CanonicalHeaderKey("ContentType"), "application/json")
				return p.Prepare(r)
			})
		}
	}

	r, _ := Prepare(&http.Request{Header: http.Header{}},
		pd())

	fmt.Printf("ContentType is %s\n", r.Header.Get("ContentType"))
	// Output: ContentType is application/json
}

// Create a sequence of three Preparers that build up the URL path.
func ExampleCreatePreparer() {
	p := CreatePreparer(
		WithBaseURL("https://microsoft.com/"),
		WithPath("a"),
		WithPath("b"),
		WithPath("c"))
	r, err := p.Prepare(&http.Request{})
	if err != nil {
		fmt.Printf("ERROR: %v\n", err)
	} else {
		fmt.Println(r.URL)
	}
	// Output: https://microsoft.com/a/b/c
}

// Create and apply separate Preparers
func ExampleCreatePreparer_multiple() {
	params := map[string]interface{}{
		"param1": "a",
		"param2": "c",
	}

	p1 := CreatePreparer(WithBaseURL("https://microsoft.com/"))
	p2 := CreatePreparer(WithPathParameters("/{param1}/b/{param2}/", params))

	r, err := p1.Prepare(&http.Request{})
	if err != nil {
		fmt.Printf("ERROR: %v\n", err)
	}

	r, err = p2.Prepare(r)
	if err != nil {
		fmt.Printf("ERROR: %v\n", err)
	} else {
		fmt.Println(r.URL)
	}
	// Output: https://microsoft.com/a/b/c/
}

// Create and chain separate Preparers
func ExampleCreatePreparer_chain() {
	params := map[string]interface{}{
		"param1": "a",
		"param2": "c",
	}

	p := CreatePreparer(WithBaseURL("https://microsoft.com/"))
	p = DecoratePreparer(p, WithPathParameters("/{param1}/b/{param2}/", params))

	r, err := p.Prepare(&http.Request{})
	if err != nil {
		fmt.Printf("ERROR: %v\n", err)
	} else {
		fmt.Println(r.URL)
	}
	// Output: https://microsoft.com/a/b/c/
}

// Create and prepare an http.Request in one call
func ExamplePrepare() {
	r, err := Prepare(&http.Request{},
		AsGet(),
		WithBaseURL("https://microsoft.com/"),
		WithPath("a/b/c/"))
	if err != nil {
		fmt.Printf("ERROR: %v\n", err)
	} else {
		fmt.Printf("%s %s", r.Method, r.URL)
	}
	// Output: GET https://microsoft.com/a/b/c/
}

// Create a request for a supplied base URL and path
func ExampleWithBaseURL() {
	r, err := Prepare(&http.Request{},
		WithBaseURL("https://microsoft.com/a/b/c/"))
	if err != nil {
		fmt.Printf("ERROR: %v\n", err)
	} else {
		fmt.Println(r.URL)
	}
	// Output: https://microsoft.com/a/b/c/
}

func TestWithBaseURL_second(t *testing.T) {
	_, err := Prepare(&http.Request{}, WithBaseURL(":"))
	if err == nil {
		t.Fatal("unexpected nil error")
	}
}

// Create a request whose Body is a byte array
func TestWithBytes(t *testing.T) {
	input := []byte{41, 82, 109}

	r, err := Prepare(&http.Request{},
		WithBytes(&input))
	if err != nil {
		t.Fatalf("ERROR: %v\n", err)
	}

	b, err := ioutil.ReadAll(r.Body)
	if err != nil {
		t.Fatalf("ERROR: %v\n", err)
	}

	if len(b) != len(input) {
		t.Fatalf("Expected the Body to contain %d bytes but got %d", len(input), len(b))
	}

	if !reflect.DeepEqual(b, input) {
		t.Fatalf("Body doesn't contain the same bytes: %s (Expected %s)", b, input)
	}
}

func ExampleWithCustomBaseURL() {
	r, err := Prepare(&http.Request{},
		WithCustomBaseURL("https://{account}.{service}.core.windows.net/",
			map[string]interface{}{
				"account": "myaccount",
				"service": "blob",
			}))
	if err != nil {
		fmt.Printf("ERROR: %v\n", err)
	} else {
		fmt.Println(r.URL)
	}
	// Output: https://myaccount.blob.core.windows.net/
}

func TestWithCustomBaseURL_second(t *testing.T) {
	_, err := Prepare(&http.Request{},
		WithCustomBaseURL(":", map[string]interface{}{}))
	if err == nil {
		t.Fatal("unexpected nil error")
	}
}

// Create a request with a custom HTTP header
func ExampleWithHeader() {
	r, err := Prepare(&http.Request{},
		WithBaseURL("https://microsoft.com/a/b/c/"),
		WithHeader("x-foo", "bar"))
	if err != nil {
		fmt.Printf("ERROR: %v\n", err)
	} else {
		fmt.Printf("Header %s=%s\n", "x-foo", r.Header.Get("x-foo"))
	}
	// Output: Header x-foo=bar
}

// Create a request whose Body is the JSON encoding of a structure
func ExampleWithFormData() {
	v := url.Values{}
	v.Add("name", "Rob Pike")
	v.Add("age", "42")

	r, err := Prepare(&http.Request{},
		WithFormData(v))
	if err != nil {
		fmt.Printf("ERROR: %v\n", err)
	}

	b, err := ioutil.ReadAll(r.Body)
	if err != nil {
		fmt.Printf("ERROR: %v\n", err)
	} else {
		fmt.Printf("Request Body contains %s\n", string(b))
	}
	// Output: Request Body contains age=42&name=Rob+Pike
}

// Create a request whose Body is the JSON encoding of a structure
func ExampleWithJSON() {
	t := mocks.T{Name: "Rob Pike", Age: 42}

	r, err := Prepare(&http.Request{},
		WithJSON(&t))
	if err != nil {
		fmt.Printf("ERROR: %v\n", err)
	}

	b, err := ioutil.ReadAll(r.Body)
	if err != nil {
		fmt.Printf("ERROR: %v\n", err)
	} else {
		fmt.Printf("Request Body contains %s\n", string(b))
	}
	// Output: Request Body contains {"name":"Rob Pike","age":42}
}

// Create a request whose Body is the XML encoding of a structure
func ExampleWithXML() {
	t := mocks.T{Name: "Rob Pike", Age: 42}

	r, err := Prepare(&http.Request{},
		WithXML(&t))
	if err != nil {
		fmt.Printf("ERROR: %v\n", err)
	}

	b, err := ioutil.ReadAll(r.Body)
	if err != nil {
		fmt.Printf("ERROR: %v\n", err)
	} else {
		fmt.Printf("Request Body contains %s\n", string(b))
	}
	// Output: Request Body contains <?xml version="1.0" encoding="UTF-8"?>
	// <T><Name>Rob Pike</Name><Age>42</Age></T>
}

// Create a request from a path with escaped parameters
func ExampleWithEscapedPathParameters() {
	params := map[string]interface{}{
		"param1": "a b c",
		"param2": "d e f",
	}
	r, err := Prepare(&http.Request{},
		WithBaseURL("https://microsoft.com/"),
		WithEscapedPathParameters("/{param1}/b/{param2}/", params))
	if err != nil {
		fmt.Printf("ERROR: %v\n", err)
	} else {
		fmt.Println(r.URL)
	}
	// Output: https://microsoft.com/a+b+c/b/d+e+f/
}

// Create a request from a path with parameters
func ExampleWithPathParameters() {
	params := map[string]interface{}{
		"param1": "a",
		"param2": "c",
	}
	r, err := Prepare(&http.Request{},
		WithBaseURL("https://microsoft.com/"),
		WithPathParameters("/{param1}/b/{param2}/", params))
	if err != nil {
		fmt.Printf("ERROR: %v\n", err)
	} else {
		fmt.Println(r.URL)
	}
	// Output: https://microsoft.com/a/b/c/
}

// Create a request with query parameters
func ExampleWithQueryParameters() {
	params := map[string]interface{}{
		"q1": []string{"value1"},
		"q2": []string{"value2"},
	}
	r, err := Prepare(&http.Request{},
		WithBaseURL("https://microsoft.com/"),
		WithPath("/a/b/c/"),
		WithQueryParameters(params))
	if err != nil {
		fmt.Printf("ERROR: %v\n", err)
	} else {
		fmt.Println(r.URL)
	}
	// Output: https://microsoft.com/a/b/c/?q1=value1&q2=value2
}

func TestWithCustomBaseURL(t *testing.T) {
	r, err := Prepare(&http.Request{}, WithCustomBaseURL("https://{account}.{service}.core.windows.net/",
		map[string]interface{}{
			"account": "myaccount",
			"service": "blob",
		}))
	if err != nil {
		t.Fatalf("autorest: WithCustomBaseURL should not fail")
	}
	if r.URL.String() != "https://myaccount.blob.core.windows.net/" {
		t.Fatalf("autorest: WithCustomBaseURL expected https://myaccount.blob.core.windows.net/, got %s", r.URL)
	}
}

func TestWithCustomBaseURLwithInvalidURL(t *testing.T) {
	_, err := Prepare(&http.Request{}, WithCustomBaseURL("hello/{account}.{service}.core.windows.net/",
		map[string]interface{}{
			"account": "myaccount",
			"service": "blob",
		}))
	if err == nil {
		t.Fatalf("autorest: WithCustomBaseURL should fail fo URL parse error")
	}
}

func TestWithPathWithInvalidPath(t *testing.T) {
	p := "path%2*end"
	if _, err := Prepare(&http.Request{}, WithBaseURL("https://microsoft.com/"), WithPath(p)); err == nil {
		t.Fatalf("autorest: WithPath should fail for invalid URL escape error for path '%v' ", p)
	}

}

func TestWithPathParametersWithInvalidPath(t *testing.T) {
	p := "path%2*end"
	m := map[string]interface{}{
		"path1": p,
	}
	if _, err := Prepare(&http.Request{}, WithBaseURL("https://microsoft.com/"), WithPathParameters("/{path1}/", m)); err == nil {
		t.Fatalf("autorest: WithPath should fail for invalid URL escape for path '%v' ", p)
	}

}

func TestCreatePreparerDoesNotModify(t *testing.T) {
	r1 := &http.Request{}
	p := CreatePreparer()
	r2, err := p.Prepare(r1)
	if err != nil {
		t.Fatalf("autorest: CreatePreparer failed (%v)", err)
	}
	if !reflect.DeepEqual(r1, r2) {
		t.Fatalf("autorest: CreatePreparer without decorators modified the request")
	}
}

func TestCreatePreparerRunsDecoratorsInOrder(t *testing.T) {
	p := CreatePreparer(WithBaseURL("https://microsoft.com/"), WithPath("1"), WithPath("2"), WithPath("3"))
	r, err := p.Prepare(&http.Request{})
	if err != nil {
		t.Fatalf("autorest: CreatePreparer failed (%v)", err)
	}
	if r.URL.String() != "https:/1/2/3" && r.URL.Host != "microsoft.com" {
		t.Fatalf("autorest: CreatePreparer failed to run decorators in order")
	}
}

func TestWithBaseURLEncodeQueryParams(t *testing.T) {
	p := CreatePreparer(WithBaseURL("https://contoso.com/path?$qp1=something/else&$qp2=do/this&$skipToken=US1:0637720028736481134:ad5b2d5b;AS1:-1:FFFFFFFF"))
	r, err := p.Prepare(&http.Request{})
	if err != nil {
		t.Fatalf("autorest: CreatePreparer failed (%v)", err)
	}
	want := "https://contoso.com/path?%24qp1=something%2Felse&%24qp2=do%2Fthis&%24skipToken=US1%3A0637720028736481134%3Aad5b2d5b%3BAS1%3A-1%3AFFFFFFFF"
	if u := r.URL.String(); u != want {
		t.Fatalf("unexpected URL, got %s, want %s", u, want)
	}
}

func TestAsContentType(t *testing.T) {
	r, err := Prepare(mocks.NewRequest(), AsContentType("application/text"))
	if err != nil {
		fmt.Printf("ERROR: %v", err)
	}
	if r.Header.Get(headerContentType) != "application/text" {
		t.Fatalf("autorest: AsContentType failed to add header (%s=%s)", headerContentType, r.Header.Get(headerContentType))
	}
}

func TestAsFormURLEncoded(t *testing.T) {
	r, err := Prepare(mocks.NewRequest(), AsFormURLEncoded())
	if err != nil {
		fmt.Printf("ERROR: %v", err)
	}
	if r.Header.Get(headerContentType) != mimeTypeFormPost {
		t.Fatalf("autorest: AsFormURLEncoded failed to add header (%s=%s)", headerContentType, r.Header.Get(headerContentType))
	}
}

func TestAsJSON(t *testing.T) {
	r, err := Prepare(mocks.NewRequest(), AsJSON())
	if err != nil {
		fmt.Printf("ERROR: %v", err)
	}
	if r.Header.Get(headerContentType) != mimeTypeJSON {
		t.Fatalf("autorest: AsJSON failed to add header (%s=%s)", headerContentType, r.Header.Get(headerContentType))
	}
}

func TestWithNothing(t *testing.T) {
	r1 := mocks.NewRequest()
	r2, err := Prepare(r1, WithNothing())
	if err != nil {
		t.Fatalf("autorest: WithNothing returned an unexpected error (%v)", err)
	}

	if !reflect.DeepEqual(r1, r2) {
		t.Fatal("azure: WithNothing modified the passed HTTP Request")
	}
}

func TestWithBearerAuthorization(t *testing.T) {
	r, err := Prepare(mocks.NewRequest(), WithBearerAuthorization("SOME-TOKEN"))
	if err != nil {
		fmt.Printf("ERROR: %v", err)
	}
	if r.Header.Get(headerAuthorization) != "Bearer SOME-TOKEN" {
		t.Fatalf("autorest: WithBearerAuthorization failed to add header (%s=%s)", headerAuthorization, r.Header.Get(headerAuthorization))
	}
}

func TestWithUserAgent(t *testing.T) {
	ua := "User Agent Go"
	r, err := Prepare(mocks.NewRequest(), WithUserAgent(ua))
	if err != nil {
		fmt.Printf("ERROR: %v", err)
	}
	if r.UserAgent() != ua || r.Header.Get(headerUserAgent) != ua {
		t.Fatalf("autorest: WithUserAgent failed to add header (%s=%s)", headerUserAgent, r.Header.Get(headerUserAgent))
	}
}

func TestWithMethod(t *testing.T) {
	r, _ := Prepare(mocks.NewRequest(), WithMethod("HEAD"))
	if r.Method != "HEAD" {
		t.Fatal("autorest: WithMethod failed to set HTTP method header")
	}
}

func TestAsDelete(t *testing.T) {
	r, _ := Prepare(mocks.NewRequest(), AsDelete())
	if r.Method != "DELETE" {
		t.Fatal("autorest: AsDelete failed to set HTTP method header to DELETE")
	}
}

func TestAsGet(t *testing.T) {
	r, _ := Prepare(mocks.NewRequest(), AsGet())
	if r.Method != "GET" {
		t.Fatal("autorest: AsGet failed to set HTTP method header to GET")
	}
}

func TestAsHead(t *testing.T) {
	r, _ := Prepare(mocks.NewRequest(), AsHead())
	if r.Method != "HEAD" {
		t.Fatal("autorest: AsHead failed to set HTTP method header to HEAD")
	}
}

func TestAsMerge(t *testing.T) {
	r, _ := Prepare(mocks.NewRequest(), AsMerge())
	if r.Method != "MERGE" {
		t.Fatal("autorest: AsMerge failed to set HTTP method header to MERGE")
	}
}

func TestAsOptions(t *testing.T) {
	r, _ := Prepare(mocks.NewRequest(), AsOptions())
	if r.Method != "OPTIONS" {
		t.Fatal("autorest: AsOptions failed to set HTTP method header to OPTIONS")
	}
}

func TestAsPatch(t *testing.T) {
	r, _ := Prepare(mocks.NewRequest(), AsPatch())
	if r.Method != "PATCH" {
		t.Fatal("autorest: AsPatch failed to set HTTP method header to PATCH")
	}
}

func TestAsPost(t *testing.T) {
	r, _ := Prepare(mocks.NewRequest(), AsPost())
	if r.Method != "POST" {
		t.Fatal("autorest: AsPost failed to set HTTP method header to POST")
	}
}

func TestAsPut(t *testing.T) {
	r, _ := Prepare(mocks.NewRequest(), AsPut())
	if r.Method != "PUT" {
		t.Fatal("autorest: AsPut failed to set HTTP method header to PUT")
	}
}

func TestPrepareWithNullRequest(t *testing.T) {
	_, err := Prepare(nil)
	if err == nil {
		t.Fatal("autorest: Prepare failed to return an error when given a null http.Request")
	}
}

func TestWithFormData(t *testing.T) {
	v := url.Values{}
	v.Add("name", "Rob Pike")
	v.Add("age", "42")

	r, err := Prepare(&http.Request{},
		WithFormData(v))
	if err != nil {
		t.Fatalf("autorest: WithFormData failed with error (%v)", err)
	}

	b, err := ioutil.ReadAll(r.Body)
	if err != nil {
		t.Fatalf("autorest: WithFormData failed with error (%v)", err)
	}

	expected := "name=Rob+Pike&age=42"
	if !(string(b) == "name=Rob+Pike&age=42" || string(b) == "age=42&name=Rob+Pike") {
		t.Fatalf("autorest:WithFormData failed to return correct string got (%v), expected (%v)", string(b), expected)
	}

	if r.ContentLength != int64(len(b)) {
		t.Fatalf("autorest:WithFormData set Content-Length to %v, expected %v", r.ContentLength, len(b))
	}

	if expected, got := r.Header.Get(http.CanonicalHeaderKey(headerContentType)), mimeTypeFormPost; expected != got {
		t.Fatalf("autorest:WithFormData Content Type not set or set to wrong value. Expected %v and got %v", expected, got)
	}
}

func TestWithMultiPartFormDataSetsContentLength(t *testing.T) {
	v := map[string]interface{}{
		"file": ioutil.NopCloser(strings.NewReader("Hello Gopher")),
		"age":  "42",
	}

	r, err := Prepare(&http.Request{},
		WithMultiPartFormData(v))
	if err != nil {
		t.Fatalf("autorest: WithMultiPartFormData failed with error (%v)", err)
	}

	b, err := ioutil.ReadAll(r.Body)
	if err != nil {
		t.Fatalf("autorest: WithMultiPartFormData failed with error (%v)", err)
	}

	if r.ContentLength != int64(len(b)) {
		t.Fatalf("autorest:WithMultiPartFormData set Content-Length to %v, expected %v", r.ContentLength, len(b))
	}
}

func TestWithMultiPartFormDataWithNoFile(t *testing.T) {
	v := map[string]interface{}{
		"file": "no file",
		"age":  "42",
	}

	r, err := Prepare(&http.Request{},
		WithMultiPartFormData(v))
	if err != nil {
		t.Fatalf("autorest: WithMultiPartFormData failed with error (%v)", err)
	}

	b, err := ioutil.ReadAll(r.Body)
	if err != nil {
		t.Fatalf("autorest: WithMultiPartFormData failed with error (%v)", err)
	}

	if r.ContentLength != int64(len(b)) {
		t.Fatalf("autorest:WithMultiPartFormData set Content-Length to %v, expected %v", r.ContentLength, len(b))
	}
}

func TestWithFile(t *testing.T) {
	r, err := Prepare(&http.Request{},
		WithFile(ioutil.NopCloser(strings.NewReader("Hello Gopher"))))
	if err != nil {
		t.Fatalf("autorest: WithFile failed with error (%v)", err)
	}

	b, err := ioutil.ReadAll(r.Body)
	if err != nil {
		t.Fatalf("autorest: WithFile failed with error (%v)", err)
	}
	if r.ContentLength != int64(len(b)) {
		t.Fatalf("autorest:WithFile set Content-Length to %v, expected %v", r.ContentLength, len(b))
	}
}

func TestWithBool_SetsTheBody(t *testing.T) {
	r, err := Prepare(&http.Request{},
		WithBool(false))
	if err != nil {
		t.Fatalf("autorest: WithBool failed with error (%v)", err)
	}

	s, err := ioutil.ReadAll(r.Body)
	if err != nil {
		t.Fatalf("autorest: WithBool failed with error (%v)", err)
	}

	if r.ContentLength != int64(len(fmt.Sprintf("%v", false))) {
		t.Fatalf("autorest: WithBool set Content-Length to %v, expected %v", r.ContentLength, int64(len(fmt.Sprintf("%v", false))))
	}

	v, err := strconv.ParseBool(string(s))
	if err != nil || v {
		t.Fatalf("autorest: WithBool incorrectly encoded the boolean as %v", s)
	}
}

func TestWithFloat32_SetsTheBody(t *testing.T) {
	r, err := Prepare(&http.Request{},
		WithFloat32(42.0))
	if err != nil {
		t.Fatalf("autorest: WithFloat32 failed with error (%v)", err)
	}

	s, err := ioutil.ReadAll(r.Body)
	if err != nil {
		t.Fatalf("autorest: WithFloat32 failed with error (%v)", err)
	}

	if r.ContentLength != int64(len(fmt.Sprintf("%v", 42.0))) {
		t.Fatalf("autorest: WithFloat32 set Content-Length to %v, expected %v", r.ContentLength, int64(len(fmt.Sprintf("%v", 42.0))))
	}

	v, err := strconv.ParseFloat(string(s), 32)
	if err != nil || float32(v) != float32(42.0) {
		t.Fatalf("autorest: WithFloat32 incorrectly encoded the boolean as %v", s)
	}
}

func TestWithFloat64_SetsTheBody(t *testing.T) {
	r, err := Prepare(&http.Request{},
		WithFloat64(42.0))
	if err != nil {
		t.Fatalf("autorest: WithFloat64 failed with error (%v)", err)
	}

	s, err := ioutil.ReadAll(r.Body)
	if err != nil {
		t.Fatalf("autorest: WithFloat64 failed with error (%v)", err)
	}

	if r.ContentLength != int64(len(fmt.Sprintf("%v", 42.0))) {
		t.Fatalf("autorest: WithFloat64 set Content-Length to %v, expected %v", r.ContentLength, int64(len(fmt.Sprintf("%v", 42.0))))
	}

	v, err := strconv.ParseFloat(string(s), 64)
	if err != nil || v != float64(42.0) {
		t.Fatalf("autorest: WithFloat64 incorrectly encoded the boolean as %v", s)
	}
}

func TestWithInt32_SetsTheBody(t *testing.T) {
	r, err := Prepare(&http.Request{},
		WithInt32(42))
	if err != nil {
		t.Fatalf("autorest: WithInt32 failed with error (%v)", err)
	}

	s, err := ioutil.ReadAll(r.Body)
	if err != nil {
		t.Fatalf("autorest: WithInt32 failed with error (%v)", err)
	}

	if r.ContentLength != int64(len(fmt.Sprintf("%v", 42))) {
		t.Fatalf("autorest: WithInt32 set Content-Length to %v, expected %v", r.ContentLength, int64(len(fmt.Sprintf("%v", 42))))
	}

	v, err := strconv.ParseInt(string(s), 10, 32)
	if err != nil || int32(v) != int32(42) {
		t.Fatalf("autorest: WithInt32 incorrectly encoded the boolean as %v", s)
	}
}

func TestWithInt64_SetsTheBody(t *testing.T) {
	r, err := Prepare(&http.Request{},
		WithInt64(42))
	if err != nil {
		t.Fatalf("autorest: WithInt64 failed with error (%v)", err)
	}

	s, err := ioutil.ReadAll(r.Body)
	if err != nil {
		t.Fatalf("autorest: WithInt64 failed with error (%v)", err)
	}

	if r.ContentLength != int64(len(fmt.Sprintf("%v", 42))) {
		t.Fatalf("autorest: WithInt64 set Content-Length to %v, expected %v", r.ContentLength, int64(len(fmt.Sprintf("%v", 42))))
	}

	v, err := strconv.ParseInt(string(s), 10, 64)
	if err != nil || v != int64(42) {
		t.Fatalf("autorest: WithInt64 incorrectly encoded the boolean as %v", s)
	}
}

func TestWithString_SetsTheBody(t *testing.T) {
	r, err := Prepare(&http.Request{},
		WithString("value"))
	if err != nil {
		t.Fatalf("autorest: WithString failed with error (%v)", err)
	}

	s, err := ioutil.ReadAll(r.Body)
	if err != nil {
		t.Fatalf("autorest: WithString failed with error (%v)", err)
	}

	if r.ContentLength != int64(len("value")) {
		t.Fatalf("autorest: WithString set Content-Length to %v, expected %v", r.ContentLength, int64(len("value")))
	}

	if string(s) != "value" {
		t.Fatalf("autorest: WithString incorrectly encoded the string as %v", s)
	}
}

func TestWithJSONSetsContentLength(t *testing.T) {
	r, err := Prepare(&http.Request{},
		WithJSON(&mocks.T{Name: "Rob Pike", Age: 42}))
	if err != nil {
		t.Fatalf("autorest: WithJSON failed with error (%v)", err)
	}

	b, err := ioutil.ReadAll(r.Body)
	if err != nil {
		t.Fatalf("autorest: WithJSON failed with error (%v)", err)
	}

	if r.ContentLength != int64(len(b)) {
		t.Fatalf("autorest:WithJSON set Content-Length to %v, expected %v", r.ContentLength, len(b))
	}
}

func TestWithHeaderAllocatesHeaders(t *testing.T) {
	r, err := Prepare(mocks.NewRequest(), WithHeader("x-foo", "bar"))
	if err != nil {
		t.Fatalf("autorest: WithHeader failed (%v)", err)
	}
	if r.Header.Get("x-foo") != "bar" {
		t.Fatalf("autorest: WithHeader failed to add header (%s=%s)", "x-foo", r.Header.Get("x-foo"))
	}
}

func TestWithPathCatchesNilURL(t *testing.T) {
	_, err := Prepare(&http.Request{}, WithPath("a"))
	if err == nil {
		t.Fatalf("autorest: WithPath failed to catch a nil URL")
	}
}

func TestWithEscapedPathParametersCatchesNilURL(t *testing.T) {
	_, err := Prepare(&http.Request{}, WithEscapedPathParameters("", map[string]interface{}{"foo": "bar"}))
	if err == nil {
		t.Fatalf("autorest: WithEscapedPathParameters failed to catch a nil URL")
	}
}

func TestWithPathParametersCatchesNilURL(t *testing.T) {
	_, err := Prepare(&http.Request{}, WithPathParameters("", map[string]interface{}{"foo": "bar"}))
	if err == nil {
		t.Fatalf("autorest: WithPathParameters failed to catch a nil URL")
	}
}

func TestWithQueryParametersCatchesNilURL(t *testing.T) {
	_, err := Prepare(&http.Request{}, WithQueryParameters(map[string]interface{}{"foo": "bar"}))
	if err == nil {
		t.Fatalf("autorest: WithQueryParameters failed to catch a nil URL")
	}
}

func TestModifyingExistingRequest(t *testing.T) {
	r, err := Prepare(mocks.NewRequestForURL("https://bing.com"), WithPath("search"), WithQueryParameters(map[string]interface{}{"q": "golang"}))
	if err != nil {
		t.Fatalf("autorest: Preparing an existing request returned an error (%v)", err)
	}
	if r.URL.Host != "bing.com" {
		t.Fatalf("autorest: Preparing an existing request failed when setting the host (%s)", r.URL)
	}

	if r.URL.Path != "/search" {
		t.Fatalf("autorest: Preparing an existing request failed when setting the path (%s)", r.URL.Path)
	}

	if r.URL.RawQuery != "q=golang" {
		t.Fatalf("autorest: Preparing an existing request failed when setting the query parameters (%s)", r.URL.RawQuery)
	}
}

func TestModifyingRequestWithExistingQueryParameters(t *testing.T) {
	r, err := Prepare(
		mocks.NewRequestForURL("https://bing.com"),
		WithPath("search"),
		WithQueryParameters(map[string]interface{}{"q": "golang the best"}),
		WithQueryParameters(map[string]interface{}{"pq": "golang+encoded"}),
		WithQueryParameters(map[string]interface{}{"zq": []string{"one", "two"}}),
	)
	if err != nil {
		t.Fatalf("autorest: Preparing an existing request returned an error (%v)", err)
	}

	if r.URL.Host != "bing.com" {
		t.Fatalf("autorest: Preparing an existing request failed when setting the host (%s)", r.URL)
	}

	if r.URL.Path != "/search" {
		t.Fatalf("autorest: Preparing an existing request failed when setting the path (%s)", r.URL.Path)
	}

	if r.URL.RawQuery != "pq=golang+encoded&q=golang+the+best&zq=one&zq=two" {
		t.Fatalf("autorest: Preparing an existing request failed when setting the query parameters (%s)", r.URL.RawQuery)
	}
}

func TestGetPrepareDecorators(t *testing.T) {
	pd := GetPrepareDecorators(context.Background())
	if l := len(pd); l != 0 {
		t.Fatalf("expected zero length but got %d", l)
	}
	pd = GetPrepareDecorators(context.Background(), WithNothing(), AsFormURLEncoded())
	if l := len(pd); l != 2 {
		t.Fatalf("expected length of two but got %d", l)
	}
}

func TestWithPrepareDecorators(t *testing.T) {
	ctx := WithPrepareDecorators(context.Background(), []PrepareDecorator{WithUserAgent("somestring")})
	pd := GetPrepareDecorators(ctx)
	if l := len(pd); l != 1 {
		t.Fatalf("expected length of one but got %d", l)
	}
	pd = GetPrepareDecorators(ctx, WithNothing(), WithNothing())
	if l := len(pd); l != 1 {
		t.Fatalf("expected length of one but got %d", l)
	}
}