@@ -914,6 +914,12 @@ func TestGetFiles(t *testing.T) {
914
914
}
915
915
}
916
916
917
+ type roundTripperFunc func (* http.Request ) (* http.Response , error )
918
+
919
+ func (f roundTripperFunc ) RoundTrip (r * http.Request ) (* http.Response , error ) {
920
+ return f (r )
921
+ }
922
+
917
923
func TestProvider_checkWebhookSecretValidity (t * testing.T ) {
918
924
t1 := time .Date (1999 , time .February , 3 , 4 , 5 , 6 , 7 , time .UTC )
919
925
cw := clockwork .NewFakeClockAt (t1 )
@@ -925,7 +931,9 @@ func TestProvider_checkWebhookSecretValidity(t *testing.T) {
925
931
expHeaderSet bool
926
932
apiNotEnabled bool
927
933
wantLogSnippet string
928
- report500 bool
934
+ statusCode int
935
+ wantNilSCIM bool
936
+ wantNilResp bool
929
937
}{
930
938
{
931
939
name : "remaining scim calls" ,
@@ -950,6 +958,22 @@ func TestProvider_checkWebhookSecretValidity(t *testing.T) {
950
958
name : "no header mean unlimited" ,
951
959
remaining : 5 ,
952
960
},
961
+ {
962
+ name : "skipping api rate limit is not enabled" ,
963
+ remaining : 0 ,
964
+ statusCode : http .StatusNotFound ,
965
+ },
966
+ {
967
+ name : "skipping because scim is not available" ,
968
+ remaining : 0 ,
969
+ wantNilSCIM : true ,
970
+ },
971
+ {
972
+ name : "resp is nil" ,
973
+ remaining : 0 ,
974
+ wantNilResp : true ,
975
+ wantSubErr : "error making request to the GitHub API checking rate limit" ,
976
+ },
953
977
{
954
978
name : "no header but no remaining scim calls" ,
955
979
remaining : 0 ,
@@ -958,7 +982,12 @@ func TestProvider_checkWebhookSecretValidity(t *testing.T) {
958
982
{
959
983
name : "api error" ,
960
984
wantSubErr : "error making request to the GitHub API checking rate limit" ,
961
- report500 : true ,
985
+ statusCode : http .StatusInternalServerError ,
986
+ },
987
+ {
988
+ name : "not enabled" ,
989
+ apiNotEnabled : true ,
990
+ wantLogSnippet : "skipping checking" ,
962
991
},
963
992
{
964
993
name : "not enabled" ,
@@ -974,14 +1003,15 @@ func TestProvider_checkWebhookSecretValidity(t *testing.T) {
974
1003
975
1004
if ! tt .apiNotEnabled {
976
1005
mux .HandleFunc ("/rate_limit" , func (rw http.ResponseWriter , _ * http.Request ) {
977
- if tt .report500 {
978
- rw .WriteHeader (http . StatusInternalServerError )
1006
+ if tt .statusCode != 0 {
1007
+ rw .WriteHeader (tt . statusCode )
979
1008
return
980
1009
}
981
- s := & github.RateLimits {
982
- SCIM : & github.Rate {
1010
+ s := & github.RateLimits {}
1011
+ if ! tt .wantNilSCIM {
1012
+ s .SCIM = & github.Rate {
983
1013
Remaining : tt .remaining ,
984
- },
1014
+ }
985
1015
}
986
1016
st := new (struct {
987
1017
Resources * github.RateLimits `json:"resources"`
@@ -996,6 +1026,16 @@ func TestProvider_checkWebhookSecretValidity(t *testing.T) {
996
1026
})
997
1027
}
998
1028
defer teardown ()
1029
+
1030
+ // create bad round tripper to make response nil and test that it handles that case.
1031
+ if tt .wantNilResp {
1032
+ errRT := roundTripperFunc (func (* http.Request ) (* http.Response , error ) {
1033
+ return nil , fmt .Errorf ("network down" )
1034
+ })
1035
+ httpClient := & http.Client {Transport : errRT }
1036
+ fakeclient = github .NewClient (httpClient )
1037
+ }
1038
+
999
1039
v := & Provider {
1000
1040
ghClient : fakeclient ,
1001
1041
Logger : logger ,
0 commit comments