File: gorequest_test.go

package info (click to toggle)
golang-github-parnurzeal-gorequest 0.2.15-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 176 kB
  • sloc: makefile: 4
file content (1776 lines) | stat: -rw-r--r-- 59,808 bytes parent folder | download | duplicates (2)
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
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
package gorequest

import (
	"bytes"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/http/httptest"
	"net/url"
	"reflect"
	"strconv"
	"strings"
	"testing"
	"time"

	"mime/multipart"

	"os"

	"github.com/elazarl/goproxy"
)

type (
	heyYou struct {
		Hey string `json:"hey"`
	}
	testStruct struct {
		String      string
		Int         int
		Btrue       bool
		Bfalse      bool
		Float       float64
		StringArray []string
		IntArray    []int
		BoolArray   []bool
		FloatArray  []float64
	}
)

// Test for changeMapToURLValues
func TestChangeMapToURLValues(t *testing.T) {

	data := map[string]interface{}{
		"s":  "a string",
		"i":  42,
		"bt": true,
		"bf": false,
		"f":  12.345,
		"sa": []string{"s1", "s2"},
		"ia": []int{47, 73},
		"fa": []float64{1.23, 4.56},
		"ba": []bool{true, false},
	}

	urlValues := changeMapToURLValues(data)

	var (
		s  string
		sd string
	)

	if s := urlValues.Get("s"); s != data["s"] {
		t.Errorf("Expected string %v, got %v", data["s"], s)
	}

	s = urlValues.Get("i")
	sd = strconv.Itoa(data["i"].(int))
	if s != sd {
		t.Errorf("Expected int %v, got %v", sd, s)
	}

	s = urlValues.Get("bt")
	sd = strconv.FormatBool(data["bt"].(bool))
	if s != sd {
		t.Errorf("Expected boolean %v, got %v", sd, s)
	}

	s = urlValues.Get("bf")
	sd = strconv.FormatBool(data["bf"].(bool))
	if s != sd {
		t.Errorf("Expected boolean %v, got %v", sd, s)
	}

	s = urlValues.Get("f")
	sd = strconv.FormatFloat(data["f"].(float64), 'f', -1, 64)
	if s != sd {
		t.Errorf("Expected float %v, got %v", data["f"], s)
	}

	// array cases
	// "To access multiple values, use the map directly."

	if size := len(urlValues["sa"]); size != 2 {
		t.Fatalf("Expected length %v, got %v", 2, size)
	}
	if urlValues["sa"][0] != "s1" {
		t.Errorf("Expected string %v, got %v", "s1", urlValues["sa"][0])
	}
	if urlValues["sa"][1] != "s2" {
		t.Errorf("Expected string %v, got %v", "s2", urlValues["sa"][1])
	}

	if size := len(urlValues["ia"]); size != 2 {
		t.Fatalf("Expected length %v, got %v", 2, size)
	}
	if urlValues["ia"][0] != "47" {
		t.Errorf("Expected string %v, got %v", "47", urlValues["ia"][0])
	}
	if urlValues["ia"][1] != "73" {
		t.Errorf("Expected string %v, got %v", "73", urlValues["ia"][1])
	}

	if size := len(urlValues["ba"]); size != 2 {
		t.Fatalf("Expected length %v, got %v", 2, size)
	}
	if urlValues["ba"][0] != "true" {
		t.Errorf("Expected string %v, got %v", "true", urlValues["ba"][0])
	}
	if urlValues["ba"][1] != "false" {
		t.Errorf("Expected string %v, got %v", "false", urlValues["ba"][1])
	}

	if size := len(urlValues["fa"]); size != 2 {
		t.Fatalf("Expected length %v, got %v", 2, size)
	}
	if urlValues["fa"][0] != "1.23" {
		t.Errorf("Expected string %v, got %v", "true", urlValues["fa"][0])
	}
	if urlValues["fa"][1] != "4.56" {
		t.Errorf("Expected string %v, got %v", "false", urlValues["fa"][1])
	}
}

// Test for Make request
func TestMakeRequest(t *testing.T) {
	var err error
	var cases = []struct {
		m string
		s *SuperAgent
	}{
		{POST, New().Post("/")},
		{GET, New().Get("/")},
		{HEAD, New().Head("/")},
		{PUT, New().Put("/")},
		{PATCH, New().Patch("/")},
		{DELETE, New().Delete("/")},
		{OPTIONS, New().Options("/")},
		{"TRACE", New().CustomMethod("TRACE", "/")}, // valid HTTP 1.1 method, see W3C RFC 2616
	}

	for _, c := range cases {
		_, err = c.s.MakeRequest()
		if err != nil {
			t.Errorf("Expected nil error for method %q; got %q", c.m, err.Error())
		}
	}

	// empty method should fail
	_, err = New().CustomMethod("", "/").MakeRequest()
	if err == nil {
		t.Errorf("Expected non-nil error for empty method; got %q", err.Error())
	}
}

// testing for Get method
func TestGet(t *testing.T) {
	const case1_empty = "/"
	const case2_set_header = "/set_header"
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// check method is GET before going to check other features
		if r.Method != GET {
			t.Errorf("Expected method %q; got %q", GET, r.Method)
		}
		if r.Header == nil {
			t.Error("Expected non-nil request Header")
		}
		switch r.URL.Path {
		default:
			t.Errorf("No testing for this case yet : %q", r.URL.Path)
		case case1_empty:
			t.Logf("case %v ", case1_empty)
		case case2_set_header:
			t.Logf("case %v ", case2_set_header)
			if r.Header.Get("API-Key") != "fookey" {
				t.Errorf("Expected 'API-Key' == %q; got %q", "fookey", r.Header.Get("API-Key"))
			}
		}
	}))

	defer ts.Close()

	New().Get(ts.URL + case1_empty).
		End()

	New().Get(ts.URL+case2_set_header).
		Set("API-Key", "fookey").
		End()
}

// testing for Get method with retry option
func TestRetryGet(t *testing.T) {
	const (
		case1_empty                         = "/"
		case24_after_3_attempt_return_valid = "/retry_3_attempt_then_valid"
		retry_count_expected                = "3"
	)

	var attempt int

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// check method is GET before going to check other features
		if r.Method != GET {
			t.Errorf("Expected method %q; got %q", GET, r.Method)
		}

		//set return status

		if r.Header == nil {
			t.Error("Expected non-nil request Header")
		}
		switch r.URL.Path {
		default:
			t.Errorf("No testing for this case yet : %q", r.URL.Path)
		case case1_empty:
			w.WriteHeader(400)
			t.Logf("case %v ", case1_empty)
		case case24_after_3_attempt_return_valid:
			if attempt == 3 {
				w.WriteHeader(200)
			} else {
				w.WriteHeader(400)
				t.Logf("case %v ", case24_after_3_attempt_return_valid)
			}
			attempt++
		}

	}))

	defer ts.Close()

	resp, _, errs := New().Get(ts.URL+case1_empty).
		Retry(3, 1*time.Nanosecond, http.StatusBadRequest).
		End()
	if errs != nil {
		t.Errorf("No testing for this case yet : %q", errs)
	}

	retryCountReturn := resp.Header.Get("Retry-Count")
	if retryCountReturn != retry_count_expected {
		t.Errorf("Expected [%s] retry but was [%s]", retry_count_expected, retryCountReturn)
	}

	resp, _, errs = New().Get(ts.URL+case24_after_3_attempt_return_valid).
		Retry(4, 1*time.Nanosecond, http.StatusBadRequest).
		End()
	if errs != nil {
		t.Errorf("No testing for this case yet : %q", errs)
	}

	retryCountReturn = resp.Header.Get("Retry-Count")
	if retryCountReturn != retry_count_expected {
		t.Errorf("Expected [%s] retry but was [%s]", retry_count_expected, retryCountReturn)
	}
}

// testing for Options method
func TestOptions(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// check method is OPTIONS before going to check other features
		if r.Method != OPTIONS {
			t.Errorf("Expected method %q; got %q", OPTIONS, r.Method)
		}
		t.Log("test Options")
		w.Header().Set("Allow", "HEAD, GET")
		w.WriteHeader(204)
	}))

	defer ts.Close()

	New().Options(ts.URL).
		End()
}

// testing that resp.Body is reusable
func TestResetBody(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Just some text"))
	}))

	defer ts.Close()

	resp, _, _ := New().Get(ts.URL).End()
	bodyBytes, _ := ioutil.ReadAll(resp.Body)
	if string(bodyBytes) != "Just some text" {
		t.Error("Expected to be able to reuse the response body")
	}
}

// testing for Param method
func TestParam(t *testing.T) {
	paramCode := "123456"
	paramFields := "f1;f2;f3"

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Form.Get("code") != paramCode {
			t.Errorf("Expected 'code' == %s; got %v", paramCode, r.Form.Get("code"))
		}

		if r.Form.Get("fields") != paramFields {
			t.Errorf("Expected 'fields' == %s; got %v", paramFields, r.Form.Get("fields"))
		}
	}))

	defer ts.Close()

	New().Get(ts.URL).
		Param("code", paramCode).
		Param("fields", paramFields)
}

// testing for POST method
func TestPost(t *testing.T) {
	const case1_empty = "/"
	const case2_set_header = "/set_header"
	const case3_send_json = "/send_json"
	const case4_send_string = "/send_string"
	const case5_integration_send_json_string = "/integration_send_json_string"
	const case6_set_query = "/set_query"
	const case7_integration_send_json_struct = "/integration_send_json_struct"
	// Check that the number conversion should be converted as string not float64
	const case8_send_json_with_long_id_number = "/send_json_with_long_id_number"
	const case9_send_json_string_with_long_id_number_as_form_result = "/send_json_string_with_long_id_number_as_form_result"
	const case10_send_struct_pointer = "/send_struct_pointer"
	const case11_send_string_pointer = "/send_string_pointer"
	const case12_send_slice_string = "/send_slice_string"
	const case13_send_slice_string_pointer = "/send_slice_string_pointer"
	const case14_send_int_pointer = "/send_int_pointer"
	const case15_send_float_pointer = "/send_float_pointer"
	const case16_send_bool_pointer = "/send_bool_pointer"
	const case17_send_string_array = "/send_string_array"
	const case18_send_string_array_pointer = "/send_string_array_pointer"
	const case19_send_struct = "/send_struct"
	const case20_send_byte_char = "/send_byte_char"
	const case21_send_byte_char_pointer = "/send_byte_char_pointer"
	const case22_send_byte_int = "/send_byte_int"
	const case22_send_byte_int_pointer = "/send_byte_int_pointer"
	const case23_send_duplicate_query_params = "/send_duplicate_query_params"
	const case24_send_query_and_request_body = "/send_query_and_request_body"

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// check method is POST before going to check other features
		if r.Method != POST {
			t.Errorf("Expected method %q; got %q", POST, r.Method)
		}
		if r.Header == nil {
			t.Error("Expected non-nil request Header")
		}
		switch r.URL.Path {
		default:
			t.Errorf("No testing for this case yet : %q", r.URL.Path)
		case case1_empty:
			t.Logf("case %v ", case1_empty)
		case case2_set_header:
			t.Logf("case %v ", case2_set_header)
			if r.Header.Get("API-Key") != "fookey" {
				t.Errorf("Expected 'API-Key' == %q; got %q", "fookey", r.Header.Get("API-Key"))
			}
		case case3_send_json:
			t.Logf("case %v ", case3_send_json)
			defer r.Body.Close()
			body, _ := ioutil.ReadAll(r.Body)
			if string(body) != `{"query1":"test","query2":"test"}` {
				t.Error(`Expected Body with {"query1":"test","query2":"test"}`, "| but got", string(body))
			}
		case case4_send_string, case11_send_string_pointer:
			t.Logf("case %v ", r.URL.Path)
			if r.Header.Get("Content-Type") != "application/x-www-form-urlencoded" {
				t.Error("Expected Header Content-Type -> application/x-www-form-urlencoded", "| but got", r.Header.Get("Content-Type"))
			}
			defer r.Body.Close()
			body, _ := ioutil.ReadAll(r.Body)
			if string(body) != "query1=test&query2=test" {
				t.Error("Expected Body with \"query1=test&query2=test\"", "| but got", string(body))
			}
		case case5_integration_send_json_string:
			t.Logf("case %v ", case5_integration_send_json_string)
			defer r.Body.Close()
			body, _ := ioutil.ReadAll(r.Body)
			if string(body) != "query1=test&query2=test" {
				t.Error("Expected Body with \"query1=test&query2=test\"", "| but got", string(body))
			}
		case case6_set_query:
			t.Logf("case %v ", case6_set_query)
			v := r.URL.Query()
			if v["query1"][0] != "test" {
				t.Error("Expected query1:test", "| but got", v["query1"][0])
			}
			if v["query2"][0] != "test" {
				t.Error("Expected query2:test", "| but got", v["query2"][0])
			}
		case case7_integration_send_json_struct:
			t.Logf("case %v ", case7_integration_send_json_struct)
			defer r.Body.Close()
			body, _ := ioutil.ReadAll(r.Body)
			comparedBody := []byte(`{"Lower":{"Color":"green","Size":1.7},"Upper":{"Color":"red","Size":0},"a":"a","name":"Cindy"}`)
			if !bytes.Equal(body, comparedBody) {
				t.Errorf(`Expected correct json but got ` + string(body))
			}
		case case8_send_json_with_long_id_number:
			t.Logf("case %v ", case8_send_json_with_long_id_number)
			defer r.Body.Close()
			body, _ := ioutil.ReadAll(r.Body)
			if string(body) != `{"id":123456789,"name":"nemo"}` {
				t.Error(`Expected Body with {"id":123456789,"name":"nemo"}`, "| but got", string(body))
			}
		case case9_send_json_string_with_long_id_number_as_form_result:
			t.Logf("case %v ", case9_send_json_string_with_long_id_number_as_form_result)
			defer r.Body.Close()
			body, _ := ioutil.ReadAll(r.Body)
			if string(body) != `id=123456789&name=nemo` {
				t.Error(`Expected Body with "id=123456789&name=nemo"`, `| but got`, string(body))
			}
		case case19_send_struct, case10_send_struct_pointer:
			t.Logf("case %v ", r.URL.Path)
			defer r.Body.Close()
			body, _ := ioutil.ReadAll(r.Body)
			comparedBody := []byte(`{"Bfalse":false,"BoolArray":[true,false],"Btrue":true,"Float":12.345,"FloatArray":[1.23,4.56,7.89],"Int":42,"IntArray":[1,2],"String":"a string","StringArray":["string1","string2"]}`)
			if !bytes.Equal(body, comparedBody) {
				t.Errorf(`Expected correct json but got ` + string(body))
			}
		case case12_send_slice_string, case13_send_slice_string_pointer, case17_send_string_array, case18_send_string_array_pointer:
			t.Logf("case %v ", r.URL.Path)
			defer r.Body.Close()
			body, _ := ioutil.ReadAll(r.Body)
			comparedBody := []byte(`["string1","string2"]`)
			if !bytes.Equal(body, comparedBody) {
				t.Errorf(`Expected correct json but got ` + string(body))
			}
		case case14_send_int_pointer:
			t.Logf("case %v ", case14_send_int_pointer)
			defer r.Body.Close()
			body, _ := ioutil.ReadAll(r.Body)
			if string(body) != "42" {
				t.Error("Expected Body with \"42\"", "| but got", string(body))
			}
		case case15_send_float_pointer:
			t.Logf("case %v ", case15_send_float_pointer)
			defer r.Body.Close()
			body, _ := ioutil.ReadAll(r.Body)
			if string(body) != "12.345" {
				t.Error("Expected Body with \"12.345\"", "| but got", string(body))
			}
		case case16_send_bool_pointer:
			t.Logf("case %v ", case16_send_bool_pointer)
			defer r.Body.Close()
			body, _ := ioutil.ReadAll(r.Body)
			if string(body) != "true" {
				t.Error("Expected Body with \"true\"", "| but got", string(body))
			}
		case case20_send_byte_char, case21_send_byte_char_pointer, case22_send_byte_int, case22_send_byte_int_pointer:
			t.Logf("case %v ", r.URL.Path)
			defer r.Body.Close()
			body, _ := ioutil.ReadAll(r.Body)
			if string(body) != "71" {
				t.Error("Expected Body with \"71\"", "| but got", string(body))
			}
		case case23_send_duplicate_query_params:
			t.Logf("case %v ", case23_send_duplicate_query_params)
			defer r.Body.Close()
			body, _ := ioutil.ReadAll(r.Body)
			sbody := string(body)
			if sbody != "param=4&param=3&param=2&param=1" {
				t.Error("Expected Body \"param=4&param=3&param=2&param=1\"", "| but got", sbody)
			}
			values, _ := url.ParseQuery(sbody)
			if len(values["param"]) != 4 {
				t.Error("Expected Body with 4 params", "| but got", sbody)
			}
			if values["param"][0] != "4" || values["param"][1] != "3" || values["param"][2] != "2" || values["param"][3] != "1" {
				t.Error("Expected Body with 4 params and values", "| but got", sbody)
			}
		case case24_send_query_and_request_body:
			t.Logf("case %v ", case24_send_query_and_request_body)
			defer r.Body.Close()
			body, _ := ioutil.ReadAll(r.Body)
			sbody := string(body)
			if sbody != `{"name":"jkbbwr"}` {
				t.Error(`Expected Body "{"name":"jkbbwr"}"`, "| but got", sbody)
			}

			v := r.URL.Query()
			if v["test"][0] != "true" {
				t.Error("Expected test:true", "| but got", v["test"][0])
			}
		}
	}))
	defer ts.Close()

	New().Post(ts.URL + case1_empty).
		End()

	New().Post(ts.URL+case2_set_header).
		Set("API-Key", "fookey").
		End()

	New().Post(ts.URL + case3_send_json).
		Send(`{"query1":"test"}`).
		Send(`{"query2":"test"}`).
		End()

	New().Post(ts.URL + case4_send_string).
		Send("query1=test").
		Send("query2=test").
		End()

	New().Post(ts.URL + case5_integration_send_json_string).
		Send("query1=test").
		Send(`{"query2":"test"}`).
		End()

	/* TODO: More testing post for application/x-www-form-urlencoded
	   post.query(json), post.query(string), post.send(json), post.send(string), post.query(both).send(both)
	*/
	New().Post(ts.URL + case6_set_query).
		Query("query1=test").
		Query("query2=test").
		End()
	// TODO:
	// 1. test 2nd layer nested struct
	// 2. test lowercase won't be export to json
	// 3. test field tag change to json field name
	type Upper struct {
		Color string
		Size  int
		note  string
	}
	type Lower struct {
		Color string
		Size  float64
		note  string
	}
	type Style struct {
		Upper Upper
		Lower Lower
		Name  string `json:"name"`
	}
	myStyle := Style{Upper: Upper{Color: "red"}, Name: "Cindy", Lower: Lower{Color: "green", Size: 1.7}}
	New().Post(ts.URL + case7_integration_send_json_struct).
		Send(`{"a":"a"}`).
		Send(myStyle).
		End()

	New().Post(ts.URL + case8_send_json_with_long_id_number).
		Send(`{"id":123456789, "name":"nemo"}`).
		End()

	New().Post(ts.URL + case9_send_json_string_with_long_id_number_as_form_result).
		Type("form").
		Send(`{"id":123456789, "name":"nemo"}`).
		End()

	payload := testStruct{
		String:      "a string",
		Int:         42,
		Btrue:       true,
		Bfalse:      false,
		Float:       12.345,
		StringArray: []string{"string1", "string2"},
		IntArray:    []int{1, 2},
		BoolArray:   []bool{true, false},
		FloatArray:  []float64{1.23, 4.56, 7.89},
	}

	New().Post(ts.URL + case10_send_struct_pointer).
		Send(&payload).
		End()

	New().Post(ts.URL + case19_send_struct).
		Send(payload).
		End()

	s1 := "query1=test"
	s2 := "query2=test"
	New().Post(ts.URL + case11_send_string_pointer).
		Send(&s1).
		Send(&s2).
		End()

	New().Post(ts.URL + case12_send_slice_string).
		Send([]string{"string1", "string2"}).
		End()

	New().Post(ts.URL + case13_send_slice_string_pointer).
		Send(&[]string{"string1", "string2"}).
		End()

	i := 42
	New().Post(ts.URL + case14_send_int_pointer).
		Send(&i).
		End()

	f := 12.345
	New().Post(ts.URL + case15_send_float_pointer).
		Send(&f).
		End()

	b := true
	New().Post(ts.URL + case16_send_bool_pointer).
		Send(&b).
		End()

	var a [2]string
	a[0] = "string1"
	a[1] = "string2"
	New().Post(ts.URL + case17_send_string_array).
		Send(a).
		End()

	New().Post(ts.URL + case18_send_string_array_pointer).
		Send(&a).
		End()

	aByte := byte('G') // = 71 dec
	New().Post(ts.URL + case20_send_byte_char).
		Send(aByte).
		End()

	New().Post(ts.URL + case21_send_byte_char_pointer).
		Send(&aByte).
		End()

	iByte := byte(71) // = 'G'
	New().Post(ts.URL + case22_send_byte_int).
		Send(iByte).
		End()

	New().Post(ts.URL + case22_send_byte_int_pointer).
		Send(&iByte).
		End()

	New().Post(ts.URL + case23_send_duplicate_query_params).
		Send("param=1").
		Send("param=2").
		Send("param=3&param=4").
		End()

	data24 := struct {
		Name string `json:"name"`
	}{"jkbbwr"}
	New().Post(ts.URL + case24_send_query_and_request_body).
		Query("test=true").
		Send(data24).
		End()
}

func checkFile(t *testing.T, fileheader *multipart.FileHeader) {
	infile, err := fileheader.Open()
	if err != nil {
		t.Error(err)
	}
	defer infile.Close()
	b, err := ioutil.ReadAll(infile)
	if err != nil {
		t.Error(err)
	}
	if len(b) == 0 {
		t.Error("Expected file-content > 0", "| but got", len(b), string(b))
	}
}

// testing for POST-Request of Type multipart
func TestMultipartRequest(t *testing.T) {

	const case0_send_not_supported_filetype = "/send_not_supported_filetype"
	const case1_send_string = "/send_string"
	const case2_send_json = "/send_json"
	const case3_integration_send_json_string = "/integration_send_json_string"
	const case4_set_query = "/set_query"
	const case5_send_struct = "/send_struct"
	const case6_send_slice_string = "/send_slice_string"
	const case6_send_slice_string_with_custom_fieldname = "/send_slice_string_with_custom_fieldname"
	const case7_send_array = "/send_array"
	const case8_integration_send_json_struct = "/integration_send_json_struct"
	const case9_send_duplicate_query_params = "/send_duplicate_query_params"

	const case10_send_file_by_path = "/send_file_by_path"
	const case10a_send_file_by_path_with_name = "/send_file_by_path_with_name"
	const case10b_send_file_by_path_pointer = "/send_file_by_path_pointer"
	const case11_send_file_by_path_without_name = "/send_file_by_path_without_name"
	const case12_send_file_by_path_without_name_but_with_fieldname = "/send_file_by_path_without_name_but_with_fieldname"

	const case13_send_file_by_content_without_name = "/send_file_by_content_without_name"
	const case13a_send_file_by_content_without_name_pointer = "/send_file_by_content_without_name_pointer"
	const case14_send_file_by_content_with_name = "/send_file_by_content_with_name"

	const case15_send_file_by_content_without_name_but_with_fieldname = "/send_file_by_content_without_name_but_with_fieldname"
	const case16_send_file_by_content_with_name_and_with_fieldname = "/send_file_by_content_with_name_and_with_fieldname"

	const case17_send_file_multiple_by_path_and_content_without_name = "/send_file_multiple_by_path_and_content_without_name"
	const case18_send_file_multiple_by_path_and_content_with_name = "/send_file_multiple_by_path_and_content_with_name"
	const case19_integration_send_file_and_data = "/integration_send_file_and_data"

	const case20_send_file_as_osfile = "/send_file_as_osfile"
	const case21_send_file_as_osfile_with_name = "/send_file_as_osfile_with_name"
	const case22_send_file_as_osfile_with_name_and_with_fieldname = "/send_file_as_osfile_with_name_and_with_fieldname"

	const case23_send_file_with_file_as_fieldname = "/send_file_with_file_as_fieldname"
	const case24_send_file_with_name_with_spaces = "/send_file_with_name_with_spaces"
	const case25_send_file_with_name_with_spaces_only = "/send_file_with_name_with_spaces_only"
	const case26_send_file_with_fieldname_with_spaces = "/send_file_with_fieldname_with_spaces"
	const case27_send_file_with_fieldname_with_spaces_only = "/send_file_with_fieldname_with_spaces_only"

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// check method is POST before going to check other features
		if r.Method != POST {
			t.Errorf("Expected method %q; got %q", POST, r.Method)
		}
		if !strings.Contains(r.Header.Get("Content-Type"), "multipart/form-data") {
			t.Error("Expected Header Content-Type -> multipart/form-data", "| but got", r.Header.Get("Content-Type"))
		}
		const _24K = (1 << 20) * 24
		err := r.ParseMultipartForm(_24K)
		if err != nil {
			t.Errorf("Error: %v", err)
		}
		t.Logf("case %v ", r.URL.Path)
		switch r.URL.Path {
		default:
			t.Errorf("No testing for this case yet : %q", r.URL.Path)
		case case0_send_not_supported_filetype:
			// will be handled at place
		case case1_send_string, case2_send_json, case3_integration_send_json_string:
			if len(r.MultipartForm.Value["query1"]) != 1 {
				t.Error("Expected length of query1:test == 1", "| but got", len(r.MultipartForm.Value["query1"]))
			}
			if r.MultipartForm.Value["query1"][0] != "test" {
				t.Error("Expected query1:test", "| but got", r.MultipartForm.Value["query1"][0])
			}
			if len(r.MultipartForm.Value["query2"]) != 1 {
				t.Error("Expected length of query2:test == 1", "| but got", len(r.MultipartForm.Value["query2"]))
			}
			if r.MultipartForm.Value["query2"][0] != "test" {
				t.Error("Expected query2:test", "| but got", r.MultipartForm.Value["query2"][0])
			}
		case case4_set_query:
			v := r.URL.Query()
			if v["query1"][0] != "test" {
				t.Error("Expected query1:test", "| but got", v["query1"][0])
			}
			if v["query2"][0] != "test" {
				t.Error("Expected query2:test", "| but got", v["query2"][0])
			}
			if val, ok := r.MultipartForm.Value["query1"]; ok {
				t.Error("Expected no value", "| but got", val)
			}
			if val, ok := r.MultipartForm.Value["query2"]; ok {
				t.Error("Expected no value", "| but got", val)
			}
		case case5_send_struct:
			if r.MultipartForm.Value["String"][0] != "a string" {
				t.Error("Expected String:'a string'", "| but got", r.MultipartForm.Value["String"][0])
			}
			if r.MultipartForm.Value["Int"][0] != "42" {
				t.Error("Expected Int:42", "| but got", r.MultipartForm.Value["Int"][0])
			}
			if r.MultipartForm.Value["Btrue"][0] != "true" {
				t.Error("Expected Btrue:true", "| but got", r.MultipartForm.Value["Btrue"][0])
			}
			if r.MultipartForm.Value["Bfalse"][0] != "false" {
				t.Error("Expected Btrue:false", "| but got", r.MultipartForm.Value["Bfalse"][0])
			}
			if r.MultipartForm.Value["Float"][0] != "12.345" {
				t.Error("Expected Float:12.345", "| but got", r.MultipartForm.Value["Float"][0])
			}
			if len(r.MultipartForm.Value["StringArray"]) != 2 {
				t.Error("Expected length of StringArray:2", "| but got", len(r.MultipartForm.Value["StringArray"]))
			}
			if r.MultipartForm.Value["StringArray"][0] != "string1" {
				t.Error("Expected StringArray:string1", "| but got", r.MultipartForm.Value["StringArray"][0])
			}
			if r.MultipartForm.Value["StringArray"][1] != "string2" {
				t.Error("Expected StringArray:string2", "| but got", r.MultipartForm.Value["StringArray"][1])
			}
			if len(r.MultipartForm.Value["IntArray"]) != 2 {
				t.Error("Expected length of IntArray:2", "| but got", len(r.MultipartForm.Value["IntArray"]))
			}
			if r.MultipartForm.Value["IntArray"][0] != "1" {
				t.Error("Expected IntArray:1", "| but got", r.MultipartForm.Value["IntArray"][0])
			}
			if r.MultipartForm.Value["IntArray"][1] != "2" {
				t.Error("Expected IntArray:2", "| but got", r.MultipartForm.Value["IntArray"][1])
			}
			if len(r.MultipartForm.Value["BoolArray"]) != 2 {
				t.Error("Expected length of BoolArray:2", "| but got", len(r.MultipartForm.Value["BoolArray"]))
			}
			if r.MultipartForm.Value["BoolArray"][0] != "true" {
				t.Error("Expected BoolArray:true", "| but got", r.MultipartForm.Value["BoolArray"][0])
			}
			if r.MultipartForm.Value["BoolArray"][1] != "false" {
				t.Error("Expected BoolArray:false", "| but got", r.MultipartForm.Value["BoolArray"][1])
			}
			if len(r.MultipartForm.Value["FloatArray"]) != 3 {
				t.Error("Expected length of FloatArray:3", "| but got", len(r.MultipartForm.Value["FloatArray"]))
			}
			if r.MultipartForm.Value["FloatArray"][0] != "1.23" {
				t.Error("Expected FloatArray:1.23", "| but got", r.MultipartForm.Value["FloatArray"][0])
			}
			if r.MultipartForm.Value["FloatArray"][1] != "4.56" {
				t.Error("Expected FloatArray:4.56", "| but got", r.MultipartForm.Value["FloatArray"][1])
			}
			if r.MultipartForm.Value["FloatArray"][2] != "7.89" {
				t.Error("Expected FloatArray:7.89", "| but got", r.MultipartForm.Value["FloatArray"][2])
			}
		case case6_send_slice_string, case7_send_array:
			if len(r.MultipartForm.Value["data"]) != 1 {
				t.Error("Expected length of data:JSON == 1", "| but got", len(r.MultipartForm.Value["data"]))
			}
			if r.MultipartForm.Value["data"][0] != `["string1","string2"]` {
				t.Error(`Expected 'data' with ["string1","string2"]`, "| but got", r.MultipartForm.Value["data"][0])
			}
		case case6_send_slice_string_with_custom_fieldname:
			if len(r.MultipartForm.Value["my_custom_data"]) != 1 {
				t.Error("Expected length of my_custom_data:JSON == 1", "| but got", len(r.MultipartForm.Value["my_custom_data"]))
			}
			if r.MultipartForm.Value["my_custom_data"][0] != `["string1","string2"]` {
				t.Error(`Expected 'my_custom_data' with ["string1","string2"]`, "| but got", r.MultipartForm.Value["my_custom_data"][0])
			}
		case case8_integration_send_json_struct:
			if len(r.MultipartForm.Value["query1"]) != 1 {
				t.Error("Expected length of query1:test == 1", "| but got", len(r.MultipartForm.Value["query1"]))
			}
			if r.MultipartForm.Value["query1"][0] != "test" {
				t.Error("Expected query1:test", "| but got", r.MultipartForm.Value["query1"][0])
			}
			if r.MultipartForm.Value["hey"][0] != "hey" {
				t.Error("Expected hey:'hey'", "| but got", r.MultipartForm.Value["Hey"][0])
			}
		case case9_send_duplicate_query_params:
			if len(r.MultipartForm.Value["param"]) != 4 {
				t.Error("Expected length of param:[] == 4", "| but got", len(r.MultipartForm.Value["param"]))
			}
			if r.MultipartForm.Value["param"][0] != "4" {
				t.Error("Expected param:0:4", "| but got", r.MultipartForm.Value["param"][0])
			}
			if r.MultipartForm.Value["param"][1] != "3" {
				t.Error("Expected param:1:3", "| but got", r.MultipartForm.Value["param"][1])
			}
			if r.MultipartForm.Value["param"][2] != "2" {
				t.Error("Expected param:2:2", "| but got", r.MultipartForm.Value["param"][2])
			}
			if r.MultipartForm.Value["param"][3] != "1" {
				t.Error("Expected param:3:1", "| but got", r.MultipartForm.Value["param"][3])
			}
		case case10_send_file_by_path, case11_send_file_by_path_without_name, case14_send_file_by_content_with_name, case20_send_file_as_osfile:
			if len(r.MultipartForm.File) != 1 {
				t.Error("Expected length of files:[] == 1", "| but got", len(r.MultipartForm.File))
			}
			if r.MultipartForm.File["file1"][0].Filename != "LICENSE" {
				t.Error("Expected Filename:LICENSE", "| but got", r.MultipartForm.File["file1"][0].Filename)
			}
			if r.MultipartForm.File["file1"][0].Header["Content-Type"][0] != "application/octet-stream" {
				t.Error("Expected Header:Content-Type:application/octet-stream", "| but got", r.MultipartForm.File["file1"][0].Header["Content-Type"])
			}
			checkFile(t, r.MultipartForm.File["file1"][0])
		case case10a_send_file_by_path_with_name, case10b_send_file_by_path_pointer, case21_send_file_as_osfile_with_name:
			if len(r.MultipartForm.File) != 1 {
				t.Error("Expected length of files:[] == 1", "| but got", len(r.MultipartForm.File))
			}
			if r.MultipartForm.File["file1"][0].Filename != "MY_LICENSE" {
				t.Error("Expected Filename:MY_LICENSE", "| but got", r.MultipartForm.File["file1"][0].Filename)
			}
		case case12_send_file_by_path_without_name_but_with_fieldname:
			if len(r.MultipartForm.File) != 1 {
				t.Error("Expected length of files:[] == 1", "| but got", len(r.MultipartForm.File))
			}
			if _, ok := r.MultipartForm.File["my_fieldname"]; !ok {
				keys := reflect.ValueOf(r.MultipartForm.File).MapKeys()
				t.Error("Expected Fieldname:my_fieldname", "| but got", keys)
			}
			if r.MultipartForm.File["my_fieldname"][0].Filename != "LICENSE" {
				t.Error("Expected Filename:LICENSE", "| but got", r.MultipartForm.File["my_fieldname"][0].Filename)
			}
			if r.MultipartForm.File["my_fieldname"][0].Header["Content-Type"][0] != "application/octet-stream" {
				t.Error("Expected Header:Content-Type:application/octet-stream", "| but got", r.MultipartForm.File["my_fieldname"][0].Header["Content-Type"])
			}
			checkFile(t, r.MultipartForm.File["my_fieldname"][0])
		case case13_send_file_by_content_without_name, case13a_send_file_by_content_without_name_pointer:
			if len(r.MultipartForm.File) != 1 {
				t.Error("Expected length of files:[] == 1", "| but got", len(r.MultipartForm.File))
			}
			if r.MultipartForm.File["file1"][0].Filename != "filename" {
				t.Error("Expected Filename:filename", "| but got", r.MultipartForm.File["file1"][0].Filename)
			}
			if r.MultipartForm.File["file1"][0].Header["Content-Type"][0] != "application/octet-stream" {
				t.Error("Expected Header:Content-Type:application/octet-stream", "| but got", r.MultipartForm.File["file1"][0].Header["Content-Type"])
			}
			checkFile(t, r.MultipartForm.File["file1"][0])
		case case15_send_file_by_content_without_name_but_with_fieldname:
			if len(r.MultipartForm.File) != 1 {
				t.Error("Expected length of files:[] == 1", "| but got", len(r.MultipartForm.File))
			}
			if _, ok := r.MultipartForm.File["my_fieldname"]; !ok {
				keys := reflect.ValueOf(r.MultipartForm.File).MapKeys()
				t.Error("Expected Fieldname:my_fieldname", "| but got", keys)
			}
			if r.MultipartForm.File["my_fieldname"][0].Filename != "filename" {
				t.Error("Expected Filename:filename", "| but got", r.MultipartForm.File["my_fieldname"][0].Filename)
			}
			if r.MultipartForm.File["my_fieldname"][0].Header["Content-Type"][0] != "application/octet-stream" {
				t.Error("Expected Header:Content-Type:application/octet-stream", "| but got", r.MultipartForm.File["my_fieldname"][0].Header["Content-Type"])
			}
			checkFile(t, r.MultipartForm.File["my_fieldname"][0])
		case case16_send_file_by_content_with_name_and_with_fieldname, case22_send_file_as_osfile_with_name_and_with_fieldname:
			if len(r.MultipartForm.File) != 1 {
				t.Error("Expected length of files:[] == 1", "| but got", len(r.MultipartForm.File))
			}
			if _, ok := r.MultipartForm.File["my_fieldname"]; !ok {
				keys := reflect.ValueOf(r.MultipartForm.File).MapKeys()
				t.Error("Expected Fieldname:my_fieldname", "| but got", keys)
			}
			if r.MultipartForm.File["my_fieldname"][0].Filename != "MY_LICENSE" {
				t.Error("Expected Filename:MY_LICENSE", "| but got", r.MultipartForm.File["my_fieldname"][0].Filename)
			}
			if r.MultipartForm.File["my_fieldname"][0].Header["Content-Type"][0] != "application/octet-stream" {
				t.Error("Expected Header:Content-Type:application/octet-stream", "| but got", r.MultipartForm.File["my_fieldname"][0].Header["Content-Type"])
			}
			checkFile(t, r.MultipartForm.File["my_fieldname"][0])
		case case17_send_file_multiple_by_path_and_content_without_name:
			if len(r.MultipartForm.File) != 2 {
				t.Error("Expected length of files:[] == 2", "| but got", len(r.MultipartForm.File))
			}
			// depends on map iteration order
			if r.MultipartForm.File["file1"][0].Filename != "LICENSE" && r.MultipartForm.File["file1"][0].Filename != "filename" {
				t.Error("Expected Filename:LICENSE||filename", "| but got", r.MultipartForm.File["file1"][0].Filename)
			}
			if r.MultipartForm.File["file1"][0].Header["Content-Type"][0] != "application/octet-stream" {
				t.Error("Expected Header:Content-Type:application/octet-stream", "| but got", r.MultipartForm.File["file1"][0].Header["Content-Type"])
			}
			// depends on map iteration order
			if r.MultipartForm.File["file2"][0].Filename != "LICENSE" && r.MultipartForm.File["file2"][0].Filename != "filename" {
				t.Error("Expected Filename:LICENSE||filename", "| but got", r.MultipartForm.File["file2"][0].Filename)
			}
			if r.MultipartForm.File["file2"][0].Header["Content-Type"][0] != "application/octet-stream" {
				t.Error("Expected Header:Content-Type:application/octet-stream", "| but got", r.MultipartForm.File["file2"][0].Header["Content-Type"])
			}
			checkFile(t, r.MultipartForm.File["file1"][0])
			checkFile(t, r.MultipartForm.File["file2"][0])
		case case18_send_file_multiple_by_path_and_content_with_name:
			if len(r.MultipartForm.File) != 2 {
				t.Error("Expected length of files:[] == 2", "| but got", len(r.MultipartForm.File))
			}
			// depends on map iteration order
			if r.MultipartForm.File["file1"][0].Filename != "LICENSE" && r.MultipartForm.File["file1"][0].Filename != "MY_LICENSE" {
				t.Error("Expected Filename:LICENSE||MY_LICENSE", "| but got", r.MultipartForm.File["file1"][0].Filename)
			}
			if r.MultipartForm.File["file1"][0].Header["Content-Type"][0] != "application/octet-stream" {
				t.Error("Expected Header:Content-Type:application/octet-stream", "| but got", r.MultipartForm.File["file1"][0].Header["Content-Type"])
			}
			// depends on map iteration order
			if r.MultipartForm.File["file2"][0].Filename != "LICENSE" && r.MultipartForm.File["file2"][0].Filename != "MY_LICENSE" {
				t.Error("Expected Filename:LICENSE||MY_LICENSE", "| but got", r.MultipartForm.File["file2"][0].Filename)
			}
			if r.MultipartForm.File["file2"][0].Header["Content-Type"][0] != "application/octet-stream" {
				t.Error("Expected Header:Content-Type:application/octet-stream", "| but got", r.MultipartForm.File["file2"][0].Header["Content-Type"])
			}
			checkFile(t, r.MultipartForm.File["file1"][0])
			checkFile(t, r.MultipartForm.File["file2"][0])
		case case19_integration_send_file_and_data:
			if len(r.MultipartForm.File) != 1 {
				t.Error("Expected length of files:[] == 1", "| but got", len(r.MultipartForm.File))
			}
			if r.MultipartForm.File["file1"][0].Filename != "LICENSE" {
				t.Error("Expected Filename:LICENSE", "| but got", r.MultipartForm.File["file1"][0].Filename)
			}
			if r.MultipartForm.File["file1"][0].Header["Content-Type"][0] != "application/octet-stream" {
				t.Error("Expected Header:Content-Type:application/octet-stream", "| but got", r.MultipartForm.File["file1"][0].Header["Content-Type"])
			}
			checkFile(t, r.MultipartForm.File["file1"][0])
			if len(r.MultipartForm.Value["query1"]) != 1 {
				t.Error("Expected length of query1:test == 1", "| but got", len(r.MultipartForm.Value["query1"]))
			}
			if r.MultipartForm.Value["query1"][0] != "test" {
				t.Error("Expected query1:test", "| but got", r.MultipartForm.Value["query1"][0])
			}
		case case23_send_file_with_file_as_fieldname:
			if len(r.MultipartForm.File) != 2 {
				t.Error("Expected length of files:[] == 2", "| but got", len(r.MultipartForm.File))
			}
			if val, ok := r.MultipartForm.File["file1"]; !ok {
				t.Error("Expected file with key: file1", "| but got ", val)
			}
			if val, ok := r.MultipartForm.File["file2"]; !ok {
				t.Error("Expected file with key: file2", "| but got ", val)
			}
			if r.MultipartForm.File["file1"][0].Filename != "b.file" {
				t.Error("Expected Filename:b.file", "| but got", r.MultipartForm.File["file1"][0].Filename)
			}
			if r.MultipartForm.File["file2"][0].Filename != "LICENSE" {
				t.Error("Expected Filename:LICENSE", "| but got", r.MultipartForm.File["file2"][0].Filename)
			}
			checkFile(t, r.MultipartForm.File["file1"][0])
			checkFile(t, r.MultipartForm.File["file2"][0])
		case case24_send_file_with_name_with_spaces, case25_send_file_with_name_with_spaces_only, case27_send_file_with_fieldname_with_spaces_only:
			if len(r.MultipartForm.File) != 1 {
				t.Error("Expected length of files:[] == 1", "| but got", len(r.MultipartForm.File))
			}
			if val, ok := r.MultipartForm.File["file1"]; !ok {
				t.Error("Expected file with key: file1", "| but got ", val)
			}
			if r.MultipartForm.File["file1"][0].Filename != "LICENSE" {
				t.Error("Expected Filename:LICENSE", "| but got", r.MultipartForm.File["file1"][0].Filename)
			}
			checkFile(t, r.MultipartForm.File["file1"][0])
		case case26_send_file_with_fieldname_with_spaces:
			if len(r.MultipartForm.File) != 1 {
				t.Error("Expected length of files:[] == 1", "| but got", len(r.MultipartForm.File))
			}
			if val, ok := r.MultipartForm.File["my_fieldname"]; !ok {
				t.Error("Expected file with key: my_fieldname", "| but got ", val)
			}
			if r.MultipartForm.File["my_fieldname"][0].Filename != "LICENSE" {
				t.Error("Expected Filename:LICENSE", "| but got", r.MultipartForm.File["my_fieldname"][0].Filename)
			}
			checkFile(t, r.MultipartForm.File["my_fieldname"][0])
		}

	}))
	defer ts.Close()

	// "the zero case"
	t.Logf("case %v ", case0_send_not_supported_filetype)
	_, _, errs := New().Post(ts.URL + case0_send_not_supported_filetype).
		Type("multipart").
		SendFile(42).
		End()

	if len(errs) == 0 {
		t.Errorf("Expected error, but got nothing: %v", errs)
	}

	New().Post(ts.URL + case1_send_string).
		Type("multipart").
		Send("query1=test").
		Send("query2=test").
		End()

	New().Post(ts.URL + case2_send_json).
		Type("multipart").
		Send(`{"query1":"test"}`).
		Send(`{"query2":"test"}`).
		End()

	New().Post(ts.URL + case3_integration_send_json_string).
		Type("multipart").
		Send("query1=test").
		Send(`{"query2":"test"}`).
		End()

	New().Post(ts.URL + case4_set_query).
		Type("multipart").
		Query("query1=test").
		Query("query2=test").
		End()

	New().Post(ts.URL + case5_send_struct).
		Type("multipart").
		Send(testStruct{
			String:      "a string",
			Int:         42,
			Btrue:       true,
			Bfalse:      false,
			Float:       12.345,
			StringArray: []string{"string1", "string2"},
			IntArray:    []int{1, 2},
			BoolArray:   []bool{true, false},
			FloatArray:  []float64{1.23, 4.56, 7.89},
		}).
		End()

	New().Post(ts.URL + case6_send_slice_string).
		Type("multipart").
		Send([]string{"string1", "string2"}).
		End()

	New().Post(ts.URL+case6_send_slice_string_with_custom_fieldname).
		Type("multipart").
		Set("json_fieldname", "my_custom_data").
		Send([]string{"string1", "string2"}).
		End()

	New().Post(ts.URL + case7_send_array).
		Type("multipart").
		Send([2]string{"string1", "string2"}).
		End()

	New().Post(ts.URL + case8_integration_send_json_struct).
		Type("multipart").
		Send(`{"query1":"test"}`).
		Send(heyYou{
			Hey: "hey",
		}).
		End()

	New().Post(ts.URL + case9_send_duplicate_query_params).
		Type("multipart").
		Send("param=1").
		Send("param=2").
		Send("param=3&param=4").
		End()

	fileByPath := "./LICENSE"
	New().Post(ts.URL + case10_send_file_by_path).
		Type("multipart").
		SendFile(fileByPath).
		End()

	New().Post(ts.URL+case10a_send_file_by_path_with_name).
		Type("multipart").
		SendFile(fileByPath, "MY_LICENSE").
		End()

	New().Post(ts.URL+case10b_send_file_by_path_pointer).
		Type("multipart").
		SendFile(&fileByPath, "MY_LICENSE").
		End()

	New().Post(ts.URL+case11_send_file_by_path_without_name).
		Type("multipart").
		SendFile(fileByPath, "").
		End()

	New().Post(ts.URL+case12_send_file_by_path_without_name_but_with_fieldname).
		Type("multipart").
		SendFile(fileByPath, "", "my_fieldname").
		End()

	b, _ := ioutil.ReadFile("./LICENSE")
	New().Post(ts.URL + case13_send_file_by_content_without_name).
		Type("multipart").
		SendFile(b).
		End()

	New().Post(ts.URL + case13a_send_file_by_content_without_name_pointer).
		Type("multipart").
		SendFile(&b).
		End()

	New().Post(ts.URL+case14_send_file_by_content_with_name).
		Type("multipart").
		SendFile(b, "LICENSE").
		End()

	New().Post(ts.URL+case15_send_file_by_content_without_name_but_with_fieldname).
		Type("multipart").
		SendFile(b, "", "my_fieldname").
		End()

	New().Post(ts.URL+case16_send_file_by_content_with_name_and_with_fieldname).
		Type("multipart").
		SendFile(b, "MY_LICENSE", "my_fieldname").
		End()

	New().Post(ts.URL + case17_send_file_multiple_by_path_and_content_without_name).
		Type("multipart").
		SendFile("./LICENSE").
		SendFile(b).
		End()

	New().Post(ts.URL+case18_send_file_multiple_by_path_and_content_with_name).
		Type("multipart").
		SendFile("./LICENSE").
		SendFile(b, "MY_LICENSE").
		End()

	New().Post(ts.URL + case19_integration_send_file_and_data).
		Type("multipart").
		SendFile("./LICENSE").
		Send("query1=test").
		End()

	osFile, _ := os.Open("./LICENSE")
	New().Post(ts.URL + case20_send_file_as_osfile).
		Type("multipart").
		SendFile(osFile).
		End()

	New().Post(ts.URL+case21_send_file_as_osfile_with_name).
		Type("multipart").
		SendFile(osFile, "MY_LICENSE").
		End()

	New().Post(ts.URL+case22_send_file_as_osfile_with_name_and_with_fieldname).
		Type("multipart").
		SendFile(osFile, "MY_LICENSE", "my_fieldname").
		End()

	New().Post(ts.URL+case23_send_file_with_file_as_fieldname).
		Type("multipart").
		SendFile(b, "b.file").
		SendFile(osFile, "", "file").
		End()

	New().Post(ts.URL+case24_send_file_with_name_with_spaces).
		Type("multipart").
		SendFile(osFile, " LICENSE  ").
		End()

	New().Post(ts.URL+case25_send_file_with_name_with_spaces_only).
		Type("multipart").
		SendFile(osFile, "   ").
		End()

	New().Post(ts.URL+case26_send_file_with_fieldname_with_spaces).
		Type("multipart").
		SendFile(osFile, "", " my_fieldname  ").
		End()

	New().Post(ts.URL+case27_send_file_with_fieldname_with_spaces_only).
		Type("multipart").
		SendFile(osFile, "", "   ").
		End()
}

// testing for Patch method
func TestPatch(t *testing.T) {
	const case1_empty = "/"
	const case2_set_header = "/set_header"
	const case3_send_json = "/send_json"
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// check method is PATCH before going to check other features
		if r.Method != PATCH {
			t.Errorf("Expected method %q; got %q", PATCH, r.Method)
		}
		if r.Header == nil {
			t.Error("Expected non-nil request Header")
		}
		switch r.URL.Path {
		default:
			t.Errorf("No testing for this case yet : %q", r.URL.Path)
		case case1_empty:
			t.Logf("case %v ", case1_empty)
		case case2_set_header:
			t.Logf("case %v ", case2_set_header)
			if r.Header.Get("API-Key") != "fookey" {
				t.Errorf("Expected 'API-Key' == %q; got %q", "fookey", r.Header.Get("API-Key"))
			}
		case case3_send_json:
			t.Logf("case %v ", case3_send_json)
			defer r.Body.Close()
			body, _ := ioutil.ReadAll(r.Body)
			if string(body) != `{"query1":"test","query2":"test"}` {
				t.Error(`Expected Body with {"query1":"test","query2":"test"}`, "| but got", string(body))
			}
		}
	}))

	defer ts.Close()

	New().Patch(ts.URL + case1_empty).
		End()

	New().Patch(ts.URL+case2_set_header).
		Set("API-Key", "fookey").
		End()

	New().Patch(ts.URL + case3_send_json).
		Send(`{"query1":"test"}`).
		Send(`{"query2":"test"}`).
		End()
}

func checkQuery(t *testing.T, q map[string][]string, key string, want string) {
	v, ok := q[key]
	if !ok {
		t.Error(key, "Not Found")
	} else if len(v) < 1 {
		t.Error("No values for", key)
	} else if v[0] != want {
		t.Errorf("Expected %v:%v | but got %v", key, want, v[0])
	}
	return
}

// TODO: more check on url query (all testcases)
func TestQueryFunc(t *testing.T) {
	const case1_send_string = "/send_string"
	const case2_send_struct = "/send_struct"
	const case3_send_string_with_duplicates = "/send_string_with_duplicates"
	const case4_send_map = "/send_map"
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != POST {
			t.Errorf("Expected method %q; got %q", POST, r.Method)
		}
		if r.Header == nil {
			t.Error("Expected non-nil request Header")
		}
		v := r.URL.Query()

		switch r.URL.Path {
		default:
			t.Errorf("No testing for this case yet : %q", r.URL.Path)
		case case1_send_string, case2_send_struct:
			checkQuery(t, v, "query1", "test1")
			checkQuery(t, v, "query2", "test2")
		case case3_send_string_with_duplicates:
			checkQuery(t, v, "query1", "test1")
			checkQuery(t, v, "query2", "test2")

			if len(v["param"]) != 4 {
				t.Errorf("Expected Body with 4 params | but got %q", len(v["param"]))
			}
			if v["param"][0] != "1" || v["param"][1] != "2" || v["param"][2] != "3" || v["param"][3] != "4" {
				t.Error("Expected Body with 4 params and values", "| but got", r.URL.RawQuery)
			}
		case case4_send_map:
			checkQuery(t, v, "query1", "test1")
			checkQuery(t, v, "query2", "test2")
			checkQuery(t, v, "query3", "3.1415926")
			checkQuery(t, v, "query4", "true")
		}
	}))
	defer ts.Close()

	New().Post(ts.URL + case1_send_string).
		Query("query1=test1").
		Query("query2=test2").
		End()

	qq := struct {
		Query1 string
		Query2 string
	}{
		Query1: "test1",
		Query2: "test2",
	}
	New().Post(ts.URL + case2_send_struct).
		Query(qq).
		End()

	New().Post(ts.URL + case3_send_string_with_duplicates).
		Query("query1=test1").
		Query("query2=test2").
		Query("param=1").
		Query("param=2").
		Query("param=3&param=4").
		End()

	New().Post(ts.URL + case4_send_map).
		Query(map[string]interface{}{
			"query1": "test1",
			"query2": "test2",
			"query3": 3.1415926,
			"query4": true,
		}).
		End()
}

// TODO: more tests on redirect
func TestRedirectPolicyFunc(t *testing.T) {
	redirectSuccess := false
	redirectFuncGetCalled := false
	tsRedirect := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		redirectSuccess = true
	}))
	defer tsRedirect.Close()
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		http.Redirect(w, r, tsRedirect.URL, http.StatusMovedPermanently)
	}))
	defer ts.Close()

	New().
		Get(ts.URL).
		RedirectPolicy(func(req Request, via []Request) error {
			redirectFuncGetCalled = true
			return nil
		}).End()
	if !redirectSuccess {
		t.Error("Expected reaching another redirect url not original one")
	}
	if !redirectFuncGetCalled {
		t.Error("Expected redirect policy func to get called")
	}
}

func TestEndBytes(t *testing.T) {
	serverOutput := "hello world"
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(200)
		w.Write([]byte(serverOutput))
	}))
	defer ts.Close()

	// Callback.
	{
		resp, bodyBytes, errs := New().Get(ts.URL).EndBytes(func(resp Response, body []byte, errs []error) {
			if len(errs) > 0 {
				t.Fatalf("Unexpected errors: %s", errs)
			}
			if resp.StatusCode != 200 {
				t.Fatalf("Expected StatusCode=200, actual StatusCode=%v", resp.StatusCode)
			}
			if string(body) != serverOutput {
				t.Errorf("Expected bodyBytes=%s, actual bodyBytes=%s", serverOutput, string(body))
			}
		})
		if len(errs) > 0 {
			t.Fatalf("Unexpected errors: %s", errs)
		}
		if resp.StatusCode != 200 {
			t.Fatalf("Expected StatusCode=200, actual StatusCode=%v", resp.StatusCode)
		}
		if string(bodyBytes) != serverOutput {
			t.Errorf("Expected bodyBytes=%s, actual bodyBytes=%s", serverOutput, string(bodyBytes))
		}
	}

	// No callback.
	{
		resp, bodyBytes, errs := New().Get(ts.URL).EndBytes()
		if len(errs) > 0 {
			t.Errorf("Unexpected errors: %s", errs)
		}
		if resp.StatusCode != 200 {
			t.Errorf("Expected StatusCode=200, actual StatusCode=%v", resp.StatusCode)
		}
		if string(bodyBytes) != serverOutput {
			t.Errorf("Expected bodyBytes=%s, actual bodyBytes=%s", serverOutput, string(bodyBytes))
		}
	}
}

func TestEndStruct(t *testing.T) {
	var resStruct heyYou
	expStruct := heyYou{Hey: "you"}
	serverOutput, err := json.Marshal(expStruct)
	if err != nil {
		t.Errorf("Unexpected errors: %s", err)
	}
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(200)
		w.Write(serverOutput)
	}))
	defer ts.Close()

	// Callback.
	{
		resp, bodyBytes, errs := New().Get(ts.URL).EndStruct(func(resp Response, v interface{}, body []byte, errs []error) {
			if len(errs) > 0 {
				t.Fatalf("Unexpected errors: %s", errs)
			}
			if resp.StatusCode != 200 {
				t.Fatalf("Expected StatusCode=200, actual StatusCode=%v", resp.StatusCode)
			}
			if !reflect.DeepEqual(expStruct, resStruct) {
				resBytes, _ := json.Marshal(resStruct)
				t.Errorf("Expected body=%s, actual bodyBytes=%s", serverOutput, string(resBytes))
			}
			if !reflect.DeepEqual(body, serverOutput) {
				t.Errorf("Expected bodyBytes=%s, actual bodyBytes=%s", serverOutput, string(body))
			}
		})
		if len(errs) > 0 {
			t.Fatalf("Unexpected errors: %s", errs)
		}
		if resp.StatusCode != 200 {
			t.Fatalf("Expected StatusCode=200, actual StatusCode=%v", resp.StatusCode)
		}
		if !reflect.DeepEqual(bodyBytes, serverOutput) {
			t.Errorf("Expected bodyBytes=%s, actual bodyBytes=%s", serverOutput, string(bodyBytes))
		}
	}

	// No callback.
	{
		resp, bodyBytes, errs := New().Get(ts.URL).EndStruct(&resStruct)
		if len(errs) > 0 {
			t.Errorf("Unexpected errors: %s", errs)
		}
		if resp.StatusCode != 200 {
			t.Errorf("Expected StatusCode=200, actual StatusCode=%v", resp.StatusCode)
		}
		if !reflect.DeepEqual(expStruct, resStruct) {
			resBytes, _ := json.Marshal(resStruct)
			t.Errorf("Expected body=%s, actual bodyBytes=%s", serverOutput, string(resBytes))
		}
		if !reflect.DeepEqual(bodyBytes, serverOutput) {
			t.Errorf("Expected bodyBytes=%s, actual bodyBytes=%s", serverOutput, string(bodyBytes))
		}
	}
}

func TestProxyFunc(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "proxy passed")
	}))
	defer ts.Close()
	// start proxy
	proxy := goproxy.NewProxyHttpServer()
	proxy.OnRequest().DoFunc(
		func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
			return r, nil
		})
	ts2 := httptest.NewServer(proxy)
	// sending request via Proxy
	resp, body, _ := New().Proxy(ts2.URL).Get(ts.URL).End()
	if resp.StatusCode != 200 {
		t.Error("Expected 200 Status code")
	}
	if body != "proxy passed" {
		t.Error("Expected 'proxy passed' body string")
	}
}

func TestTimeoutFunc(t *testing.T) {
	// 1st case, dial timeout
	startTime := time.Now()
	_, _, errs := New().Timeout(1000 * time.Millisecond).Get("http://www.google.com:81").End()
	elapsedTime := time.Since(startTime)
	if errs == nil {
		t.Error("Expected dial timeout error but get nothing")
	}
	if elapsedTime < 1000*time.Millisecond || elapsedTime > 1500*time.Millisecond {
		t.Errorf("Expected timeout in between 1000 -> 1500 ms | but got %d", elapsedTime)
	}
	// 2st case, read/write timeout (Can dial to url but want to timeout because too long operation on the server)
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		time.Sleep(1100 * time.Millisecond) // slightly longer than expected
		w.WriteHeader(200)
	}))
	request := New().Timeout(1000 * time.Millisecond)
	startTime = time.Now()
	_, _, errs = request.Get(ts.URL).End()
	elapsedTime = time.Since(startTime)
	if errs == nil {
		t.Error("Expected dial+read/write timeout | but get nothing")
	}
	if elapsedTime < 1000*time.Millisecond || elapsedTime > 1500*time.Millisecond {
		t.Errorf("Expected timeout in between 1000 -> 1500 ms | but got %d", elapsedTime)
	}
	// 3rd case, testing reuse of same request
	ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		time.Sleep(1100 * time.Millisecond) // slightly longer than expected
		w.WriteHeader(200)
	}))
	startTime = time.Now()
	_, _, errs = request.Get(ts.URL).End()
	elapsedTime = time.Since(startTime)
	if errs == nil {
		t.Error("Expected dial+read/write timeout | but get nothing")
	}
	if elapsedTime < 1000*time.Millisecond || elapsedTime > 1500*time.Millisecond {
		t.Errorf("Expected timeout in between 1000 -> 1500 ms | but got %d", elapsedTime)
	}

}

func TestCookies(t *testing.T) {
	request := New().Timeout(60 * time.Second)
	_, _, errs := request.Get("https://github.com").End()
	if errs != nil {
		t.Error("Cookies test request did not complete")
		return
	}
	domain, _ := url.Parse("https://github.com")
	if len(request.Client.Jar.Cookies(domain)) == 0 {
		t.Error("Expected cookies | but get nothing")
	}
}

func TestGetSetCookie(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != GET {
			t.Errorf("Expected method %q; got %q", GET, r.Method)
		}
		c, err := r.Cookie("API-Cookie-Name")
		if err != nil {
			t.Error(err)
		}
		if c == nil {
			t.Error("Expected non-nil request Cookie 'API-Cookie-Name'")
		} else if c.Value != "api-cookie-value" {
			t.Errorf("Expected 'API-Cookie-Name' == %q; got %q", "api-cookie-value", c.Value)
		}
	}))
	defer ts.Close()

	New().Get(ts.URL).
		AddCookie(&http.Cookie{Name: "API-Cookie-Name", Value: "api-cookie-value"}).
		End()
}

func TestGetSetCookies(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != GET {
			t.Errorf("Expected method %q; got %q", GET, r.Method)
		}
		c, err := r.Cookie("API-Cookie-Name1")
		if err != nil {
			t.Error(err)
		}
		if c == nil {
			t.Error("Expected non-nil request Cookie 'API-Cookie-Name1'")
		} else if c.Value != "api-cookie-value1" {
			t.Errorf("Expected 'API-Cookie-Name1' == %q; got %q", "api-cookie-value1", c.Value)
		}
		c, err = r.Cookie("API-Cookie-Name2")
		if err != nil {
			t.Error(err)
		}
		if c == nil {
			t.Error("Expected non-nil request Cookie 'API-Cookie-Name2'")
		} else if c.Value != "api-cookie-value2" {
			t.Errorf("Expected 'API-Cookie-Name2' == %q; got %q", "api-cookie-value2", c.Value)
		}
	}))
	defer ts.Close()

	New().Get(ts.URL).AddCookies([]*http.Cookie{
		{Name: "API-Cookie-Name1", Value: "api-cookie-value1"},
		{Name: "API-Cookie-Name2", Value: "api-cookie-value2"},
	}).End()
}

func TestErrorTypeWrongKey(t *testing.T) {
	//defer afterTest(t)
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "Hello, checkTypeWrongKey")
	}))
	defer ts.Close()

	_, _, err := New().
		Get(ts.URL).
		Type("wrongtype").
		End()
	if len(err) != 0 {
		if err[0].Error() != "Type func: incorrect type \"wrongtype\"" {
			t.Errorf("Wrong error message: " + err[0].Error())
		}
	} else {
		t.Error("Should have error")
	}
}

func TestBasicAuth(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		auth := strings.SplitN(r.Header["Authorization"][0], " ", 2)
		if len(auth) != 2 || auth[0] != "Basic" {
			t.Error("bad syntax")
		}
		payload, _ := base64.StdEncoding.DecodeString(auth[1])
		pair := strings.SplitN(string(payload), ":", 2)
		if pair[0] != "myusername" || pair[1] != "mypassword" {
			t.Error("Wrong username/password")
		}
	}))
	defer ts.Close()
	New().Post(ts.URL).
		SetBasicAuth("myusername", "mypassword").
		End()
}

func TestXml(t *testing.T) {
	xml := `<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>`

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// check method is PATCH before going to check other features
		if r.Method != POST {
			t.Errorf("Expected method %q; got %q", POST, r.Method)
		}
		if r.Header == nil {
			t.Error("Expected non-nil request Header")
		}

		if r.Header.Get("Content-Type") != "application/xml" {
			t.Error("Expected Header Content-Type -> application/xml", "| but got", r.Header.Get("Content-Type"))
		}

		defer r.Body.Close()
		body, _ := ioutil.ReadAll(r.Body)
		if string(body) != xml {
			t.Error(`Expected XML `, xml, "| but got", string(body))
		}
	}))

	defer ts.Close()

	New().Post(ts.URL).
		Type("xml").
		Send(xml).
		End()

	New().Post(ts.URL).
		Set("Content-Type", "application/xml").
		Send(xml).
		End()
}

func TestPlainText(t *testing.T) {
	text := `hello world \r\n I am GoRequest`

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// check method is PATCH before going to check other features
		if r.Method != POST {
			t.Errorf("Expected method %q; got %q", POST, r.Method)
		}
		if r.Header == nil {
			t.Error("Expected non-nil request Header")
		}
		if r.Header.Get("Content-Type") != "text/plain" {
			t.Error("Expected Header Content-Type -> text/plain", "| but got", r.Header.Get("Content-Type"))
		}

		defer r.Body.Close()
		body, _ := ioutil.ReadAll(r.Body)
		if string(body) != text {
			t.Error(`Expected text `, text, "| but got", string(body))
		}
	}))

	defer ts.Close()

	New().Post(ts.URL).
		Type("text").
		Send(text).
		End()

	New().Post(ts.URL).
		Set("Content-Type", "text/plain").
		Send(text).
		End()
}

func TestAsCurlCommand(t *testing.T) {
	var (
		endpoint = "http://github.com/parnurzeal/gorequest"
		jsonData = `{"here": "is", "some": {"json": ["data"]}}`
	)

	request := New().Timeout(10*time.Second).Put(endpoint).Set("Content-Type", "application/json").Send(jsonData)

	curlComand, err := request.AsCurlCommand()
	if err != nil {
		t.Fatal(err)
	}

	expected := fmt.Sprintf(`curl -X 'PUT' -d '%v' -H 'Content-Type: application/json' '%v'`, strings.Replace(jsonData, " ", "", -1), endpoint)
	if curlComand != expected {
		t.Fatalf("\nExpected curlCommand=%v\n   but actual result=%v", expected, curlComand)
	}
}

func TestSetDebugByEnvironmentVar(t *testing.T) {
	endpoint := "http://github.com/parnurzeal/gorequest"

	var buf bytes.Buffer
	logger := log.New(&buf, "[gorequest]", log.LstdFlags)

	os.Setenv("GOREQUEST_DEBUG", "1")
	New().SetLogger(logger).Get(endpoint).End()

	if len(buf.String()) == 0 {
		t.Fatalf("\nExpected gorequest to log request and response object if GOREQUEST_DEBUG=1")
	}

	os.Setenv("GOREQUEST_DEBUG", "")
	buf.Reset()

	New().SetLogger(logger).Get(endpoint).End()

	if len(buf.String()) > 0 {
		t.Fatalf("\nExpected gorequest not to log request and response object if GOREQUEST_DEBUG is not set.")
	}
}