github.com/letsencrypt/boulder@v0.20251208.0/observer/probers/http/http_conf_test.go (about) 1 package probers 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/letsencrypt/boulder/observer/probers" 8 "github.com/letsencrypt/boulder/test" 9 "gopkg.in/yaml.v3" 10 ) 11 12 func TestHTTPConf_MakeProber(t *testing.T) { 13 type fields struct { 14 URL string 15 RCodes []int 16 } 17 tests := []struct { 18 name string 19 fields fields 20 wantErr bool 21 }{ 22 // valid 23 {"valid fqdn valid rcode", fields{"http://example.com", []int{200}}, false}, 24 {"valid hostname valid rcode", fields{"example", []int{200}}, true}, 25 // invalid 26 {"valid fqdn no rcode", fields{"http://example.com", nil}, true}, 27 {"valid fqdn invalid rcode", fields{"http://example.com", []int{1000}}, true}, 28 {"valid fqdn 1 invalid rcode", fields{"http://example.com", []int{200, 1000}}, true}, 29 {"bad fqdn good rcode", fields{":::::", []int{200}}, true}, 30 {"missing scheme", fields{"example.com", []int{200}}, true}, 31 } 32 for _, tt := range tests { 33 t.Run(tt.name, func(t *testing.T) { 34 c := HTTPConf{ 35 URL: tt.fields.URL, 36 RCodes: tt.fields.RCodes, 37 } 38 if _, err := c.MakeProber(nil); (err != nil) != tt.wantErr { 39 t.Errorf("HTTPConf.Validate() error = %v, wantErr %v", err, tt.wantErr) 40 } 41 }) 42 } 43 } 44 45 func TestHTTPConf_UnmarshalSettings(t *testing.T) { 46 type fields struct { 47 url any 48 rcodes any 49 useragent any 50 insecure any 51 } 52 tests := []struct { 53 name string 54 fields fields 55 want probers.Configurer 56 wantErr bool 57 }{ 58 {"valid", fields{"google.com", []int{200}, "boulder_observer", false}, HTTPConf{"google.com", []int{200}, "boulder_observer", false}, false}, 59 {"invalid", fields{42, 42, 42, 42}, nil, true}, 60 } 61 for _, tt := range tests { 62 t.Run(tt.name, func(t *testing.T) { 63 settings := probers.Settings{ 64 "url": tt.fields.url, 65 "rcodes": tt.fields.rcodes, 66 "useragent": tt.fields.useragent, 67 "insecure": tt.fields.insecure, 68 } 69 settingsBytes, _ := yaml.Marshal(settings) 70 c := HTTPConf{} 71 got, err := c.UnmarshalSettings(settingsBytes) 72 if (err != nil) != tt.wantErr { 73 t.Errorf("DNSConf.UnmarshalSettings() error = %v, wantErr %v", err, tt.wantErr) 74 return 75 } 76 if !reflect.DeepEqual(got, tt.want) { 77 t.Errorf("DNSConf.UnmarshalSettings() = %v, want %v", got, tt.want) 78 } 79 }) 80 } 81 } 82 83 func TestHTTPProberName(t *testing.T) { 84 // Test with blank `useragent` 85 proberYAML := ` 86 url: https://www.google.com 87 rcodes: [ 200 ] 88 useragent: "" 89 insecure: true 90 ` 91 c := HTTPConf{} 92 configurer, err := c.UnmarshalSettings([]byte(proberYAML)) 93 test.AssertNotError(t, err, "Got error for valid prober config") 94 prober, err := configurer.MakeProber(nil) 95 test.AssertNotError(t, err, "Got error for valid prober config") 96 test.AssertEquals(t, prober.Name(), "https://www.google.com-[200]-letsencrypt/boulder-observer-http-client-insecure") 97 98 // Test with custom `useragent` 99 proberYAML = ` 100 url: https://www.google.com 101 rcodes: [ 200 ] 102 useragent: fancy-custom-http-client 103 ` 104 c = HTTPConf{} 105 configurer, err = c.UnmarshalSettings([]byte(proberYAML)) 106 test.AssertNotError(t, err, "Got error for valid prober config") 107 prober, err = configurer.MakeProber(nil) 108 test.AssertNotError(t, err, "Got error for valid prober config") 109 test.AssertEquals(t, prober.Name(), "https://www.google.com-[200]-fancy-custom-http-client") 110 111 }