-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce tests for the internal package
- Loading branch information
1 parent
cb7be38
commit b2ae948
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package internal_test | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/google-gemini/proxy-to-gemini/internal" | ||
) | ||
|
||
func TestErrorHandler(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
method string | ||
code int | ||
msg string | ||
arg []interface{} | ||
wantBody string | ||
wantLog string | ||
}{ | ||
{ | ||
name: "Bad request without args", | ||
method: http.MethodPost, | ||
code: http.StatusBadRequest, | ||
msg: "failed to read request body", | ||
wantBody: "failed to read request body\n", | ||
}, | ||
{ | ||
name: "Internal server error with args", | ||
method: http.MethodGet, | ||
code: http.StatusInternalServerError, | ||
msg: "failed to generate content: %v", | ||
arg: []interface{}{fmt.Errorf("generic error")}, | ||
wantBody: "failed to generate content: generic error\n", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
recorder := httptest.NewRecorder() | ||
|
||
req := httptest.NewRequest(tt.method, "/", nil) | ||
|
||
internal.ErrorHandler(recorder, req, tt.code, tt.msg, tt.arg...) | ||
|
||
if recorder.Code != tt.code { | ||
t.Errorf("got status %v, want %v", recorder.Code, tt.code) | ||
} | ||
|
||
if recorder.Body.String() != tt.wantBody { | ||
t.Errorf("got body %v, want %v", recorder.Body.String(), tt.wantBody) | ||
} | ||
}) | ||
} | ||
} |