@@ -1106,6 +1106,51 @@ func TestDo_redirectLoop(t *testing.T) {
1106
1106
}
1107
1107
}
1108
1108
1109
+ func TestDo_preservesResponseInHTTPError (t * testing.T ) {
1110
+ t .Parallel ()
1111
+ client , mux , _ := setup (t )
1112
+
1113
+ mux .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
1114
+ w .Header ().Set ("Content-Type" , "application/json" )
1115
+ w .WriteHeader (http .StatusNotFound )
1116
+ fmt .Fprintf (w , `{
1117
+ "message": "Resource not found",
1118
+ "documentation_url": "https://docs.github.com/rest/reference/repos#get-a-repository"
1119
+ }` )
1120
+ })
1121
+
1122
+ req , _ := client .NewRequest ("GET" , "." , nil )
1123
+ var resp * Response
1124
+ var data interface {}
1125
+ resp , err := client .Do (context .Background (), req , & data )
1126
+
1127
+ if err == nil {
1128
+ t .Fatal ("Expected error response" )
1129
+ }
1130
+
1131
+ // Verify error type and access to status code
1132
+ errResp , ok := err .(* ErrorResponse )
1133
+ if ! ok {
1134
+ t .Fatalf ("Expected *ErrorResponse error, got %T" , err )
1135
+ }
1136
+
1137
+ // Verify status code is accessible from both Response and ErrorResponse
1138
+ if resp == nil {
1139
+ t .Fatal ("Expected response to be returned even with error" )
1140
+ }
1141
+ if got , want := resp .StatusCode , http .StatusNotFound ; got != want {
1142
+ t .Errorf ("Response status = %d, want %d" , got , want )
1143
+ }
1144
+ if got , want := errResp .Response .StatusCode , http .StatusNotFound ; got != want {
1145
+ t .Errorf ("Error response status = %d, want %d" , got , want )
1146
+ }
1147
+
1148
+ // Verify error contains proper message
1149
+ if ! strings .Contains (errResp .Message , "Resource not found" ) {
1150
+ t .Errorf ("Error message = %q, want to contain 'Resource not found'" , errResp .Message )
1151
+ }
1152
+ }
1153
+
1109
1154
// Test that an error caused by the internal http client's Do() function
1110
1155
// does not leak the client secret.
1111
1156
func TestDo_sanitizeURL (t * testing.T ) {
0 commit comments