File tree Expand file tree Collapse file tree 3 files changed +41
-3
lines changed Expand file tree Collapse file tree 3 files changed +41
-3
lines changed Original file line number Diff line number Diff line change
1
+ ### Fixed
2
+
3
+ - GGClient no longer crashes when it receives an odd server response, like a response with no Content-Type header.
Original file line number Diff line number Diff line change @@ -82,7 +82,7 @@ def load_detail(resp: Response) -> Detail:
82
82
:return: detail object of response
83
83
:rtype: Detail
84
84
"""
85
- if resp .headers [ "content-type" ] == "application/json" :
85
+ if resp .headers . get ( "content-type" ) == "application/json" :
86
86
data = resp .json ()
87
87
else :
88
88
data = {"detail" : resp .text }
@@ -96,7 +96,7 @@ def is_ok(resp: Response) -> bool:
96
96
and the content type is JSON.
97
97
"""
98
98
return (
99
- resp .headers [ "content-type" ] == "application/json"
99
+ resp .headers . get ( "content-type" ) == "application/json"
100
100
and resp .status_code == codes .ok
101
101
)
102
102
@@ -107,7 +107,7 @@ def is_create_ok(resp: Response) -> bool:
107
107
and the content type is JSON.
108
108
"""
109
109
return (
110
- resp .headers [ "content-type" ] == "application/json"
110
+ resp .headers . get ( "content-type" ) == "application/json"
111
111
and resp .status_code == codes .created
112
112
)
113
113
Original file line number Diff line number Diff line change @@ -1113,3 +1113,38 @@ def test_sca_client_scan_diff_with_params(client: GGClient):
1113
1113
1114
1114
assert vyper_vulns is not None
1115
1115
assert all (vuln .severity in ("high" , "critical" ) for vuln in vyper_vulns .vulns )
1116
+
1117
+
1118
+ def test_is_ok_bad_response ():
1119
+ """
1120
+ GIVEN a 500 response with no content-type header
1121
+ WHEN is_ok() is called
1122
+ THEN it does not fail
1123
+ AND returns false
1124
+ """
1125
+ resp = Mock ()
1126
+ resp .headers = {}
1127
+ resp .status_code = 500
1128
+ resp .text = "Failed"
1129
+
1130
+ assert not is_ok (resp )
1131
+
1132
+
1133
+ @responses .activate
1134
+ def test_read_metadata_bad_response (client : GGClient ):
1135
+ """
1136
+ GIVEN a /metadata endpoint that returns a 500 error with no content-type
1137
+ THEN a call to read_metadata() does not fail
1138
+ AND returns a valid Detail instance
1139
+ """
1140
+ mock_response = responses .get (
1141
+ url = client ._url_from_endpoint ("metadata" , "v1" ),
1142
+ status = 500 ,
1143
+ body = "Failed" ,
1144
+ )
1145
+
1146
+ detail = client .read_metadata ()
1147
+
1148
+ assert mock_response .call_count == 1
1149
+ assert detail .status_code == 500
1150
+ assert detail .detail == "Failed"
You can’t perform that action at this time.
0 commit comments