Files
fission-src/pkg/utils/httpserver/server_test.go
T
Sanket SudakeandGitHub 3b4211b581 Update go dependencies to latest and actions used in workflows (#2510)
* Updated all Go language dependencies to latest version available
* Formatted all files as per gofmt
* Update Golangci-lint version to 1.48.0
* Updated action version wherer application in Github workflows
* Updated Kubernetes version to latest available
* Remove "io/ioutil" references and replace with "io"/"os"

Signed-off-by: Sanket Sudake <sanketsudake@gmail.com>
2022-08-19 13:36:37 +05:30

63 lines
1.5 KiB
Go

package httpserver
import (
"context"
"io"
"net/http"
"testing"
"github.com/gorilla/mux"
"go.uber.org/zap"
"github.com/fission/fission/pkg/utils/loggerfactory"
)
func TestStartServer(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
logger := loggerfactory.GetLogger()
m := mux.NewRouter()
m.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("test handler"))
if err != nil {
logger.Error("failed to write response", zap.Error(err))
}
}))
go StartServer(ctx, logger, "test", "8999", m)
tests := []struct {
URL string
StatusCode int
Body string
}{
{
URL: "http://localhost:8999",
StatusCode: http.StatusOK,
Body: "test handler",
},
{
URL: "http://localhost:8999/notfound",
StatusCode: http.StatusNotFound,
Body: "404 page not found\n",
},
}
for _, test := range tests {
resp, err := http.Get(test.URL)
if err != nil {
t.Errorf("failed to make get request %v: %v", test.URL, err)
}
defer resp.Body.Close()
if resp.StatusCode != test.StatusCode {
t.Errorf("expected status code %v, got %v", test.StatusCode, resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Errorf("failed to read response body: %v", err)
}
if string(body) != test.Body {
t.Errorf("expected body \"%v\", got \"%v\"", test.Body, string(body))
}
}
}