Skip to content

Commit

Permalink
Introduce tests for the internal package
Browse files Browse the repository at this point in the history
  • Loading branch information
Stavrospanakakis committed Sep 17, 2024
1 parent cb7be38 commit b2ae948
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions internal/internal_test.go
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)
}
})
}
}

0 comments on commit b2ae948

Please sign in to comment.