dumb/handlers/instances_test.go

42 lines
842 B
Go
Raw Normal View History

2024-03-06 20:53:29 +01:00
package handlers
import (
"encoding/json"
"net/http"
"net/http/httptest"
2024-05-02 21:29:50 +01:00
"os"
2024-03-06 20:53:29 +01:00
"testing"
2024-05-02 21:29:50 +01:00
"github.com/rramiachraf/dumb/utils"
2024-03-06 20:53:29 +01:00
)
func TestInstancesList(t *testing.T) {
r, err := http.NewRequest(http.MethodGet, "/instances.json", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
2024-05-02 21:29:50 +01:00
l := utils.NewLogger(os.Stdout)
2024-03-06 20:53:29 +01:00
m := New(l)
m.ServeHTTP(rr, r)
c := rr.Result().Header.Get("content-type")
if c != ContentTypeJSON {
t.Fatalf("expected %q, got %q", ContentTypeJSON, c)
}
defer rr.Result().Body.Close()
d := json.NewDecoder(rr.Result().Body)
instances := []map[string]any{}
if err := d.Decode(&instances); err != nil {
t.Fatalf("unable to decode json from response, %q\n", err)
}
if _, exists := instances[0]["clearnet"]; !exists {
t.Fatal("unable to get clearnet value from instances list")
}
}