@@ -952,6 +952,12 @@ func TestGetFiles(t *testing.T) {
952
952
}
953
953
}
954
954
955
+ type roundTripperFunc func (* http.Request ) (* http.Response , error )
956
+
957
+ func (f roundTripperFunc ) RoundTrip (r * http.Request ) (* http.Response , error ) {
958
+ return f (r )
959
+ }
960
+
955
961
func TestProvider_checkWebhookSecretValidity (t * testing.T ) {
956
962
t1 := time .Date (1999 , time .February , 3 , 4 , 5 , 6 , 7 , time .UTC )
957
963
cw := clockwork .NewFakeClockAt (t1 )
@@ -963,7 +969,9 @@ func TestProvider_checkWebhookSecretValidity(t *testing.T) {
963
969
expHeaderSet bool
964
970
apiNotEnabled bool
965
971
wantLogSnippet string
966
- report500 bool
972
+ statusCode int
973
+ wantNilSCIM bool
974
+ wantNilResp bool
967
975
}{
968
976
{
969
977
name : "remaining scim calls" ,
@@ -988,6 +996,22 @@ func TestProvider_checkWebhookSecretValidity(t *testing.T) {
988
996
name : "no header mean unlimited" ,
989
997
remaining : 5 ,
990
998
},
999
+ {
1000
+ name : "skipping api rate limit is not enabled" ,
1001
+ remaining : 0 ,
1002
+ statusCode : http .StatusNotFound ,
1003
+ },
1004
+ {
1005
+ name : "skipping because scim is not available" ,
1006
+ remaining : 0 ,
1007
+ wantNilSCIM : true ,
1008
+ },
1009
+ {
1010
+ name : "resp is nil" ,
1011
+ remaining : 0 ,
1012
+ wantNilResp : true ,
1013
+ wantSubErr : "error making request to the GitHub API checking rate limit" ,
1014
+ },
991
1015
{
992
1016
name : "no header but no remaining scim calls" ,
993
1017
remaining : 0 ,
@@ -996,7 +1020,12 @@ func TestProvider_checkWebhookSecretValidity(t *testing.T) {
996
1020
{
997
1021
name : "api error" ,
998
1022
wantSubErr : "error making request to the GitHub API checking rate limit" ,
999
- report500 : true ,
1023
+ statusCode : http .StatusInternalServerError ,
1024
+ },
1025
+ {
1026
+ name : "not enabled" ,
1027
+ apiNotEnabled : true ,
1028
+ wantLogSnippet : "skipping checking" ,
1000
1029
},
1001
1030
{
1002
1031
name : "not enabled" ,
@@ -1012,14 +1041,15 @@ func TestProvider_checkWebhookSecretValidity(t *testing.T) {
1012
1041
1013
1042
if ! tt .apiNotEnabled {
1014
1043
mux .HandleFunc ("/rate_limit" , func (rw http.ResponseWriter , _ * http.Request ) {
1015
- if tt .report500 {
1016
- rw .WriteHeader (http . StatusInternalServerError )
1044
+ if tt .statusCode != 0 {
1045
+ rw .WriteHeader (tt . statusCode )
1017
1046
return
1018
1047
}
1019
- s := & github.RateLimits {
1020
- SCIM : & github.Rate {
1048
+ s := & github.RateLimits {}
1049
+ if ! tt .wantNilSCIM {
1050
+ s .SCIM = & github.Rate {
1021
1051
Remaining : tt .remaining ,
1022
- },
1052
+ }
1023
1053
}
1024
1054
st := new (struct {
1025
1055
Resources * github.RateLimits `json:"resources"`
@@ -1034,6 +1064,16 @@ func TestProvider_checkWebhookSecretValidity(t *testing.T) {
1034
1064
})
1035
1065
}
1036
1066
defer teardown ()
1067
+
1068
+ // create bad round tripper to make response nil and test that it handles that case.
1069
+ if tt .wantNilResp {
1070
+ errRT := roundTripperFunc (func (* http.Request ) (* http.Response , error ) {
1071
+ return nil , fmt .Errorf ("network down" )
1072
+ })
1073
+ httpClient := & http.Client {Transport : errRT }
1074
+ fakeclient = github .NewClient (httpClient )
1075
+ }
1076
+
1037
1077
v := & Provider {
1038
1078
ghClient : fakeclient ,
1039
1079
Logger : logger ,
0 commit comments