-
Notifications
You must be signed in to change notification settings - Fork 11
/
config_test.go
1581 lines (1398 loc) · 55.4 KB
/
config_test.go
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
package j8a
import (
"fmt"
"os"
"reflect"
"strconv"
"testing"
isd "github.com/jbenet/go-is-domain"
)
func TestMain(m *testing.M) {
testSetup()
code := m.Run()
os.Exit(code)
}
func testSetup() {
os.Setenv("LOGLEVEL", "TRACE")
os.Setenv("LOGCOLOR", "true")
}
func TestTimeZones(t *testing.T) {
tzs := []struct {
tz string
valid bool
}{
{
"Australia/Sydney", true,
},
{
"Africa/Johannesburg", true,
},
{
"UTC", true,
},
}
config := new(Config)
for _, tz := range tzs {
config.TimeZone = tz.tz
config.validateTimeZone()
}
}
// TestDefaultDownstreamReadTimeout
func TestDefaultDownstreamReadTimeout(t *testing.T) {
config := new(Config).setDefaultDownstreamParams()
got := config.Connection.Downstream.ReadTimeoutSeconds
want := 5
if got != want {
t.Errorf("default config got %d, want %d", got, want)
}
}
// TestDefaultDownstreamIdleTimeout
func TestDefaultDownstreamIdleTimeout(t *testing.T) {
config := new(Config).setDefaultDownstreamParams()
got := config.Connection.Downstream.IdleTimeoutSeconds
want := 5
if got != want {
t.Errorf("default config got %d, want %d", got, want)
}
}
// TestDefaultDownstreamRoundtripTimeout
func TestDefaultDownstreamRoundtripTimeout(t *testing.T) {
config := new(Config).setDefaultDownstreamParams()
got := config.Connection.Downstream.RoundTripTimeoutSeconds
want := 10
if got != want {
t.Errorf("default config got %d, want %d", got, want)
}
}
func TestDefaultDownstreamMaxBodyBytes(t *testing.T) {
config := new(Config).setDefaultDownstreamParams()
got := config.Connection.Downstream.MaxBodyBytes
want := int64(2097152)
if got != want {
t.Errorf("default dwn max body bytes got %d, want %d", got, want)
}
}
// TestDefaultUpstreamSocketTimeout
func TestDefaultUpstreamSocketTimeout(t *testing.T) {
config := new(Config).setDefaultUpstreamParams()
got := config.Connection.Upstream.SocketTimeoutSeconds
want := 3
if got != want {
t.Errorf("default config got %d, want %d", got, want)
}
}
// TestDefaultUpstreamSocketTimeout
func TestDefaultUpstreamReadTimeout(t *testing.T) {
config := new(Config).setDefaultUpstreamParams()
got := config.Connection.Upstream.ReadTimeoutSeconds
want := 10
if got != want {
t.Errorf("default config got %d, want %d", got, want)
}
}
// TestDefaultUpstreamIdleTimeout
func TestDefaultUpstreamIdleTimeout(t *testing.T) {
config := new(Config).setDefaultUpstreamParams()
got := config.Connection.Upstream.IdleTimeoutSeconds
want := 120
if got != want {
t.Errorf("default config got %d, want %d", got, want)
}
}
// TestDefaultUpstreamConnectionPoolSize
func TestDefaultUpstreamConnectionPoolSize(t *testing.T) {
config := new(Config).setDefaultUpstreamParams()
got := config.Connection.Upstream.PoolSize
want := 32768
if got != want {
t.Errorf("default config got %d, want %d", got, want)
}
}
// TestDefaultUpstreamConnectionMaxAttempts
func TestDefaultUpstreamConnectionMaxAttempts(t *testing.T) {
config := new(Config).setDefaultUpstreamParams()
got := config.Connection.Upstream.MaxAttempts
want := 1
if got != want {
t.Errorf("default config got %d, want %d", got, want)
}
}
// TestDefaultPolicy
func TestDefaultPolicy(t *testing.T) {
wantLabel := "default"
config := new(Config).addDefaultPolicy()
def := config.Policies[wantLabel]
gotLabel := def[0].Label
if gotLabel != wantLabel {
t.Errorf("default policy label got %s, want %s", gotLabel, wantLabel)
}
gotWeight := def[0].Weight
wantWeight := 1.0
if gotWeight != wantWeight {
t.Errorf("default policy weight got %f, want %fc", gotWeight, wantWeight)
}
}
func TestParsePolicy(t *testing.T) {
configJson := []byte("{\"policies\": {\n\t\t\"ab\": [{\n\t\t\t\t\"label\": \"green\",\n\t\t\t\t\"weight\": 0.8\n\t\t\t},\n\t\t\t{\n\t\t\t\t\"label\": \"blue\",\n\t\t\t\t\"weight\": 0.2\n\t\t\t}\n\t\t]\n\t}}")
config := new(Config).parse(configJson)
p := config.Policies
gp0l := p["ab"][0].Label
wp0l := "green"
if gp0l != wp0l {
t.Errorf("incorrectly parsed policy label, want %s, got %s", wp0l, gp0l)
}
gp0w := p["ab"][0].Weight
wp0w := 0.8
if gp0w != wp0w {
t.Errorf("incorrectly parsed policy weight, want %f, got %f", wp0w, gp0w)
}
gp1l := p["ab"][1].Label
wp1l := "blue"
if gp1l != wp1l {
t.Errorf("incorrectly parsed policy label, want %s, got %s", wp1l, gp1l)
}
gp1w := p["ab"][1].Weight
wp1w := 0.2
if gp1w != wp1w {
t.Errorf("incorrectly parsed policy weight, want %f, got %f", wp1w, gp1w)
}
}
// TestParseResource
func TestParseResource(t *testing.T) {
configJson := []byte("{\n\t\"resources\": {\n\t\t\"customer\": [{\n\t\t\t\"labels\": [\n\t\t\t\t\"blue\"\n\t\t\t],\n\t\t\t\"url\": {\n\t\t\t\t\"scheme\": \"http\",\n\t\t\t\t\"host\": \"localhost\",\n\t\t\t\t\"port\": \"8081\"\n\t\t\t}\n\t\t}]\n\t}\n}")
config := new(Config).parse(configJson)
config.reApplyResourceNames()
customer := config.Resources["customer"]
if customer[0].Name != "customer" {
t.Error("resource name not re-applied after parsing server configuration. cannot identify upstream resource, mapping would fail")
}
if customer[0].Labels[0] != "blue" {
t.Error("resource label not parsed, cannot perform upstream mapping")
}
wantURL := URL{"http", "localhost", "8081"}
gotURL := customer[0].URL
if wantURL != gotURL {
t.Errorf("resource url parsed incorrectly. want %s got %s", wantURL, gotURL)
}
}
// TestParseConnection
func TestParseConnection(t *testing.T) {
configJson := []byte("{\n\t\"connection\": {\n\t\t\"downstream\": {\n\t\t\t\"readTimeoutSeconds\": 120,\n\t\t\t\"roundTripTimeoutSeconds\": 240,\n\t\t\t\"idleTimeoutSeconds\": 30,\n\t\t\t\"port\": 8080,\n\t\t\t\"mode\": \"TLS\"\n\t\t},\n\t\t\"upstream\": {\n\t\t\t\"socketTimeoutSeconds\": 3,\n\t\t\t\"readTimeoutSeconds\": 120,\n\t\t\t\"idleTimeoutSeconds\": 120,\n\t\t\t\"maxAttempts\": 4,\n\t\t\t\"poolSize\": 1024\n\t\t}\n\t}\n}")
config := new(Config).parse(configJson)
for i := 0; i < 2; i++ {
//this whole test runs twice. the first pass validates the parsing of config object. the 2nd pass
//validates the setDefaultValues() method does not inadvertently overwrite it
if i == 1 {
config = config.setDefaultDownstreamParams().setDefaultUpstreamParams()
}
c := config.Connection
wrts := 120
grts := c.Downstream.ReadTimeoutSeconds
if wrts != grts {
t.Errorf("incorrectly parsed downstream readTimeoutSeconds, want %d, got %d", wrts, grts)
}
wrtts := 240
grtts := c.Downstream.RoundTripTimeoutSeconds
if wrtts != grtts {
t.Errorf("incorrectly parsed downstream roundTripTimeoutSeconds, want %d, got %d", wrtts, grtts)
}
wits := 30
gits := c.Downstream.IdleTimeoutSeconds
if wits != gits {
t.Errorf("incorrectly parsed downstream idleTimeoutSeconds, want %d, got %d", wits, gits)
}
wuits := 120
guits := c.Upstream.IdleTimeoutSeconds
if wuits != guits {
t.Errorf("incorrectly parsed upstream idleTimeoutSeconds, want %d, got %d", wuits, guits)
}
wurts := 120
gurts := c.Upstream.ReadTimeoutSeconds
if wurts != gurts {
t.Errorf("incorrectly parsed upstream readTimeoutSeconds, want %d, got %d", wurts, gurts)
}
wusts := 3
gusts := c.Upstream.SocketTimeoutSeconds
if wusts != gusts {
t.Errorf("incorrectly parsed upstream socketTimeoutSeconds, want %d, got %d", wusts, gusts)
}
wups := 1024
gups := c.Upstream.PoolSize
if wups != gups {
t.Errorf("incorrectly parsed upstream poolSize, want %d, got %d", wups, gups)
}
wuma := 4
guma := c.Upstream.MaxAttempts
if wuma != guma {
t.Errorf("incorrectly parsed upstream maxAttempts, want %d, got %d", wuma, guma)
}
}
}
// TestParseRoute
func TestParseRoute(t *testing.T) {
configJson := []byte("{\"routes\": [{\n\t\t\t\"path\": \"/about\",\n\t\t\t\"resource\": \"aboutj8a\"\n\t\t},\n\t\t{\n\t\t\t\"path\": \"/customer\",\n\t\t\t\"resource\": \"customer\",\n\t\t\t\"policy\": \"ab\"\n\t\t}\n\t]}")
config := new(Config).parse(configJson)
customer := config.Routes[1]
if customer.Path != "/customer" {
t.Errorf("incorrectly parsed route path, want %s, got %s", "/customer", customer.Path)
}
if customer.Policy != "ab" {
t.Errorf("incorrectly parsed route policy, want %s, got %s", "ab", customer.Policy)
}
if customer.Resource != "customer" {
t.Errorf("incorrectly parsed route resource, want %s, got %s", "customer", customer.Resource)
}
}
// TestValidateAcmeEmail
func TestValidateConfigHasHTTP(t *testing.T) {
config := &Config{
Connection: Connection{
Downstream: Downstream{
Http: Http{Port: 80},
},
},
}
config = config.validateHTTPConfig()
}
func TestValidateConfigHasHttpAndTLS(t *testing.T) {
config := &Config{
Connection: Connection{
Downstream: Downstream{
Http: Http{
Port: 80,
},
Tls: Tls{
Port: 443,
},
},
},
}
config = config.validateHTTPConfig()
}
func TestValidateConfigHasTLS(t *testing.T) {
config := &Config{
Connection: Connection{
Downstream: Downstream{
Tls: Tls{
Port: 443,
},
},
},
}
config = config.validateHTTPConfig()
}
func TestValidateConfigNoHttpAndNoTLSFails(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("config should have panicked with no tls and no http")
} else {
t.Logf("normal config panic without http or tls port")
}
}()
config := &Config{
Connection: Connection{
Downstream: Downstream{},
},
}
config = config.validateHTTPConfig()
}
func TestValidateNonWildcardHostName(t *testing.T) {
var tests = []struct {
n string
h string
v bool
}{
{"valid host", "host.com", true},
{"invalid wildcard host", "*.host.com", false},
{"invalid unicode host", "höst.com", false},
{"invalid character host", "h/st.com", false},
{"invalid port spec host", "host.com:80", false},
}
tests = append(tests, DNSNameHostTestFactory()...)
for _, tt := range tests {
t.Run(tt.n, func(t *testing.T) {
rm := ResourceMapping{
URL: URL{
Scheme: "http://",
Host: tt.h,
Port: "80",
}}
got := validHostName(rm) == nil
want := tt.v
if got != want {
t.Errorf("%v got: %v want %v", tt.n, got, want)
}
})
}
}
func TestValidateIpv4AndIpv6(t *testing.T) {
var tests = []struct {
n string
h string
v bool
}{
{"valid ipv4", "10.1.1.1", true},
{"invalid ipv4 with ipv6 brackets", "[10.1.1.1]", false},
{"invalid short ipv4", "10.0.0", false},
{"invalid ipv4 with CIDR range", "10.0.0/1/24", false},
{"invalid ipv4", "300.0.0.0", false},
{"invalid ipv4 with port", "10.1.1.1:80", false},
{"valid ipv6", "::1", true},
{"valid ipv6", "[::1]", true},
{"invalid port spec host", "host.com:80", false},
}
for i, ipv4 := range ipv4s {
tests = append(tests, struct {
n string
h string
v bool
}{fmt.Sprintf("valid ipv4 %v", i), ipv4, true})
}
for i, ipv6 := range ipv4s {
tests = append(tests, struct {
n string
h string
v bool
}{fmt.Sprintf("valid ipv6 %v", i), ipv6, true})
}
for _, tt := range tests {
t.Run(tt.n, func(t *testing.T) {
rm := ResourceMapping{
URL: URL{
Scheme: "http://",
Host: tt.h,
Port: "80",
}}
got := validIpAddress(rm) == nil
want := tt.v
if got != want {
t.Errorf("%v got: %v want %v", tt.n, got, want)
}
})
}
}
func TestResourceMappingValidUpstreamResource(t *testing.T) {
var tests = []struct {
n string
s string
h string
p string
v bool
}{
//ports
{"invalid port -1", "http", "host.com", "-1", false},
{"invalid port 0", "http", "host.com", "0", false},
{"valid port", "http", "host.com", "80", true},
{"invalid port 65535", "http", "host.com", "65535", true},
{"invalid port 65536", "http", "host.com", "65536", false},
{"invalid port blah", "http", "host.com", "blah", false},
//schemes
{"valid scheme http", "Http", "host.com", "80", true},
{"valid scheme http", "http", "host.com", "80", true},
{"valid scheme http", "http://", "host.com", "80", true},
{"valid scheme http", "http:// ", "host.com", "80", true},
{"valid scheme https", "Https", "host.com", "443", true},
{"valid scheme https", "https", "host.com", "443", true},
{"valid scheme https", "https://", "host.com", "443", true},
{"valid scheme https", "https:// ", "host.com", "443", true},
{"valid scheme ws", "Ws", "host.com", "80", true},
{"valid scheme ws", "ws", "host.com", "80", true},
{"valid scheme ws", "ws://", "host.com", "80", true},
{"valid scheme ws", "ws:// ", "host.com", "80", true},
{"valid scheme wss", "WsS", "host.com", "80", true},
{"valid scheme wss", "wss", "host.com", "80", true},
{"valid scheme wss", "wsS://", "host.com", "80", true},
{"valid scheme wss", "wsS:// ", "host.com", "80", true},
//bad schemes
{"invalid scheme blah", "blah:// ", "host.com", "80", false},
{"invalid scheme gopher", "gopher:// ", "host.com", "80", false},
{"invalid scheme ftp", "ftp:// ", "host.com", "80", false},
//hosts
{"valid host", "http", "host.com", "80", true},
{"invalid wildcard host", "http", "*.host.com", "80", false},
{"invalid unicode host", "http", "höst.com", "80", false},
{"invalid character host", "http", "h/st.com", "80", false},
{"invalid port spec host", "http", "host.com:80", "80", false},
//ips
{"valid ipv4", "http", "10.1.1.1", "80", true},
{"invalid ipv4 with ipv6 brackets", "http", "[10.1.1.1]", "80", false},
{"invalid short ipv4", "http", "10.0.0", "80", false},
{"invalid ipv4 with CIDR range", "http", "10.0.0.1/24", "80", false},
{"invalid ipv4", "http", "300.0.0.0", "80", false},
{"invalid ipv4 with port", "http", "10.1.1.1:80", "80", false},
{"valid ipv6", "http", "::1", "80", true},
{"valid ipv6", "http", "[::1]", "80", true},
{"invalid port spec host", "http", "host.com:80", "80", false},
}
//more ipv4
for i, ipv4 := range ipv4s {
tests = append(tests, struct {
n string
s string
h string
p string
v bool
}{fmt.Sprintf("valid ipv4 %v", i), "http", ipv4, "80", true})
}
//more ipv6
for i, ipv6 := range ipv4s {
tests = append(tests, struct {
n string
s string
h string
p string
v bool
}{fmt.Sprintf("valid ipv6 %v", i), "https", ipv6, "443", true})
}
for _, tt := range tests {
t.Run(tt.n, func(t *testing.T) {
//for those tests that are failing.
if !tt.v {
defer func() {
if err := recover(); err != nil {
t.Logf("normal. recovered from config panic as expected")
}
}()
}
//validate resource mapping. for those that don't work, a config panic will fire that this test execution recovers.
rm := ResourceMapping{
URL: URL{
Scheme: tt.s,
Host: tt.h,
Port: tt.p,
}}
cfg := Config{Resources: map[string][]ResourceMapping{tt.n: []ResourceMapping{rm}}}
cfg.reApplyResourceNames().
reformatResourceUrlSchemes().
validateResources()
})
}
}
func TestReformatResourceUrlSchemes(t *testing.T) {
var tests = []struct {
n string
r string
a string
}{
//schemes for reformatting
{"valid http", "httP:// ", "http"},
{"valid http", "http ", "http"},
{"valid https", "httPs:// ", "https"},
{"valid https", "Https", "https"},
{"valid ws", "ws:// ", "ws"},
{"valid ws", "Ws ", "ws"},
{"valid wss", "wss ", "wss"},
}
for _, tt := range tests {
t.Run(tt.n, func(t *testing.T) {
//validate resource mapping. for those that don't work, a config panic will fire that this test execution recovers.
rm := ResourceMapping{
URL: URL{
Scheme: tt.r,
Host: "host.com",
Port: "80",
}}
cfg := Config{Resources: map[string][]ResourceMapping{tt.n: []ResourceMapping{rm}}}
cfg.reApplyResourceNames().
reformatResourceUrlSchemes()
got := cfg.Resources[tt.n][0].URL.Scheme
want := tt.a
if got != want {
t.Errorf("test %v got %v want %v", tt.n, got, want)
}
})
}
}
func TestDownstreamHttpPortInRange(t *testing.T) {
var tests = []struct {
n string
h int
t int
v bool
}{
//hosts
{"Invalid http -1 and valid 443 tls", -1, 443, false},
{"Valid http 80 and invalid -1 tls", 80, -1, false},
{"Valid http and no tls", 80, 0, true},
{"no http and valid tls", 0, 443, true},
{"no http and valid tls", 80, 443, true},
{"invalid tcp 80 for both http and tls", 80, 80, false},
{"invalid tcp 1 for both http and tls", 1, 1, false},
{"invalid tcp 80000 for http and valid port 443 for tls", 80000, 443, false},
{"valid tcp 80 for http and invalid port 80000 for tls", 80, 80000, false},
{"valid tcp 65534 for http and valid port 65535 for tls", 65534, 65535, true},
{"valid tcp 65535 for http and valid port 65536 for tls", 65535, 65536, false},
}
for _, tt := range tests {
t.Run(tt.n, func(t *testing.T) {
//defer this to catch validation errors for those tests that are supposed to fail
if !tt.v {
defer func() {
if r := recover(); r != nil {
t.Logf("normal. config panic for invalid HTTP config")
}
}()
}
config := &Config{
Connection: Connection{
Downstream: Downstream{
Http: Http{Port: tt.h},
Tls: Tls{Port: tt.t},
},
},
}
config.validateHTTPConfig()
})
}
}
func TestScheme(t *testing.T) {
var tests = []struct {
n string
s string
v bool
}{
//hosts
{"valid http", "http", true},
{"valid http", "http://", true},
{"valid http", "HTTP", true},
{"valid http", "HTTP://", true},
{"valid https", "https", true},
{"valid https", "https://", true},
{"valid https", "HTTPS", true},
{"valid https", "HTTPS://", true},
{"valid https", "HtTPs://", true},
{"valid https", "HtTPs:// ", true},
{"valid ws", "ws", true},
{"valid ws", "ws://", true},
{"valid ws", "wS", true},
{"valid wss", "wss://", true},
{"valid wss", "wsS://", true},
{"invalid gopher", "gopher://", false},
{"invalid ftp", "ftp://", false},
{"invalid blah", "blah://", false},
{"invalid blah", "blah", false},
}
for _, tt := range tests {
t.Run(tt.n, func(t *testing.T) {
got := validScheme(tt.s)
want := tt.v
if got != want {
t.Errorf("test %v got %v want %v", tt.n, got, want)
}
})
}
}
// TestValidateAcmeEmail
func TestValidateAcmeEmail(t *testing.T) {
config := &Config{
Connection: Connection{
Downstream: Downstream{
Http: Http{Port: 80},
Tls: Tls{
Acme: Acme{
Domains: []string{"adyntest.com"},
Provider: "letsencrypt",
Email: "noreply@example.org",
},
},
},
},
}
config = config.validateAcmeConfig()
}
// TestValidateAcmeEmail
func TestValidateAcmeGracePeriod30(t *testing.T) {
config := &Config{
Connection: Connection{
Downstream: Downstream{
Http: Http{Port: 80},
Tls: Tls{
Acme: Acme{
Domains: []string{"adyntest.com"},
Provider: "letsencrypt",
Email: "noreply@example.org",
GracePeriodDays: 30,
},
},
},
},
}
config = config.validateAcmeConfig()
want := 30
got := config.Connection.Downstream.Tls.Acme.GracePeriodDays
if want != got {
t.Errorf("want grace period days %v, got %v", want, got)
}
}
func TestValidateAcmeGracePeriod15(t *testing.T) {
config := &Config{
Connection: Connection{
Downstream: Downstream{
Http: Http{Port: 80},
Tls: Tls{
Acme: Acme{
Domains: []string{"adyntest.com"},
Provider: "letsencrypt",
Email: "noreply@example.org",
GracePeriodDays: 15,
},
},
},
},
}
config = config.validateAcmeConfig()
want := 15
got := config.Connection.Downstream.Tls.Acme.GracePeriodDays
if want != got {
t.Errorf("want grace period days %v, got %v", want, got)
}
}
func TestValidateDefaultAcmeGracePeriod(t *testing.T) {
config := &Config{
Connection: Connection{
Downstream: Downstream{
Http: Http{Port: 80},
Tls: Tls{
Acme: Acme{
Domains: []string{"adyntest.com"},
Provider: "letsencrypt",
Email: "noreply@example.org",
},
},
},
},
}
config = config.validateAcmeConfig()
want := 30
got := config.Connection.Downstream.Tls.Acme.GracePeriodDays
if want != got {
t.Errorf("want grace period days %v, got %v", want, got)
}
}
func TestValidateAcmeGracePeriodFailsGreater30(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("config should have panicked with 31 days acme grace period")
} else {
t.Logf("normal config panic for 31 days acme grace period")
}
}()
config := &Config{
Connection: Connection{
Downstream: Downstream{
Http: Http{Port: 80},
Tls: Tls{
Acme: Acme{
Domains: []string{"adyntest.com"},
Provider: "letsencrypt",
Email: "noreply@example.org",
GracePeriodDays: 31,
},
},
},
},
}
config = config.validateAcmeConfig()
}
// TestValidateAcmeDomainInvalidLeadingDotFails
func TestValidateAcmeDomainInvalidLeadingDotFails(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Logf("normal. config panic for invalid domain with supported provider")
}
}()
acmeConfigWith(".test.com").validateAcmeConfig()
t.Errorf("config did not panic for supported Acme provider but with missing domain")
}
// TestValidateAcmeDomainInvalidTrailingDotFails
func TestValidateAcmeDomainInvalidTrailingDotFails(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Logf("normal. config panic for invalid domain with supported provider")
}
}()
acmeConfigWith("test.com.").validateAcmeConfig()
t.Errorf("config did not panic for supported Acme provider but with missing domain")
}
// TestValidateAcmeDomainInvalidTrailingDotFails
// NOTE WE DO NOT SUPPORT WILDCART CERTS BECAUSE THEY CANNOT BE VERIFIED USING HTTP01 CHALLENGE ON LETSENCRYPT, SEE: https://letsencrypt.org/docs/faq/
func TestValidateAcmeDomainInvalidWildcartCertFails(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Logf("normal. config panic for invalid domain with supported provider")
}
}()
acmeConfigWith("*.test.com").validateAcmeConfig()
t.Errorf("config did not panic for supported Acme provider but with missing domain")
}
// TestValidateAcmeDomainValidSubdomainPasses
func TestValidateAcmeDomainValidSubdomainPasses(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("config did panic for valid subdomain and supported Acme provider")
}
}()
acmeConfigWith("subdomain.test.com").validateAcmeConfig()
t.Logf("normal. config did not panic for valid subdomain and supported Acme provider")
}
// TestValidateAcmeDomainMissingFails
func TestValidateAcmeDomainMissingFails(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Logf("normal. config panic for missing domain with supported provider")
}
}()
config := &Config{
Connection: Connection{
Downstream: Downstream{
Http: Http{Port: 80},
Tls: Tls{
Acme: Acme{
Provider: "letsencrypt",
Email: "noreply@example.org",
},
},
},
},
}
config = config.validateAcmeConfig()
t.Errorf("config did not panic for supported Acme provider but with missing domain")
}
// TestValidateAcmeProviderLetsencrypt
func TestValidateAcmeProviderLetsencrypt(t *testing.T) {
config := &Config{
Connection: Connection{
Downstream: Downstream{
Http: Http{Port: 80},
Tls: Tls{
Acme: Acme{
Domains: []string{"adyntest.com"},
Provider: "letsencrypt",
Email: "noreply@example.org",
},
},
},
},
}
config = config.validateAcmeConfig()
t.Logf("normal. no config panic for Acme provider letsencrypt")
}
// TestValidateAcmeProviderLetsencryptWithMultipleSubDomains
func TestValidateAcmeProviderLetsencryptWithMultipleSubdomains(t *testing.T) {
config := &Config{
Connection: Connection{
Downstream: Downstream{
Http: Http{Port: 80},
Tls: Tls{
Acme: Acme{
Domains: []string{"adyntest.com", "api.adyntest.com"},
Provider: "letsencrypt",
Email: "noreply@example.org",
},
},
},
},
}
config = config.validateAcmeConfig()
t.Logf("normal. no config panic for Acme provider letsencrypt")
}
// TestValidateAcmeProviderLetsencryptFailsWithOneInvalidSubDomain
func TestValidateAcmeProviderLetsencryptFailsWithOneInvalidSubDomain(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Logf("normal. config panic for illegal subdomain.")
}
}()
config := &Config{
Connection: Connection{
Downstream: Downstream{
Http: Http{Port: 80},
Tls: Tls{
Acme: Acme{
Domains: []string{"adyntest.com", "Iwannabeadomain"},
Provider: "letsencrypt",
Email: "noreply@example.org",
},
},
},
},
}
config = config.validateAcmeConfig()
t.Errorf("config did not panic for invalid subdomain")
}
// TestValidateAcmeProviderLetsencrypt
func TestValidateAcmeProviderPort80(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Error("config panic for correct config with port 80 for acme provider")
}
}()
config := &Config{
Connection: Connection{
Downstream: Downstream{
Http: Http{
Port: 80,
},
Tls: Tls{
Acme: Acme{
Domains: []string{"adyntest.com"},
Provider: "letsencrypt",
Email: "noreply@example.org",
},
},
},
},
}
config = config.validateAcmeConfig()
t.Logf("normal. no config panic for Acme provider letsencrypt with port 80")
}
// TestValidateAcmeProviderFailsWithMissingPort80
func TestValidateAcmeProviderFailsWithMissingPort80(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Logf("normal. config panic for missing port 80 with acme provider")
}
}()
config := &Config{
Connection: Connection{
Downstream: Downstream{
Tls: Tls{
Acme: Acme{
Domains: []string{"adyntest.com"},
Provider: "letsencrypt",
Email: "noreply@example.org",
},
},
},
},
}
config = config.validateAcmeConfig()
t.Error("no config panic for Acme provider without port 80 specified. should have panicked")
}
// TestValidateAcmeProviderFailsWithCertSpecified
func TestValidateAcmeProviderFailsWithCertSpecified(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Logf("normal. config panic for extra cert specified")
}
}()
config := &Config{
Connection: Connection{
Downstream: Downstream{
Http: Http{Port: 80},
Tls: Tls{
Acme: Acme{
Domains: []string{"adyntest.com"},
Provider: "letsencrypt",
Email: "noreply@example.org",
},
Cert: "iwannabeacertwheni'mbig",