-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmain_test.go
58 lines (50 loc) · 1.25 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"fmt"
"io/ioutil"
"log/slog"
"net/http"
"net/http/httptest"
"strings"
"testing"
"go.opentelemetry.io/otel/metric/noop"
"golang.org/x/oscar/internal/testutil"
)
func TestNewServer(t *testing.T) {
g := &Gaby{
ctx: context.Background(),
slog: testutil.Slogger(t),
slogLevel: new(slog.LevelVar),
meter: noop.Meter{},
}
// create in-memory test server
report := func(err error, _ *http.Request) { t.Error(err) }
mux := g.newServer(report)
s := httptest.NewServer(mux)
defer s.Close()
read := func(r *http.Response) string {
b, _ := ioutil.ReadAll(r.Body)
r.Body.Close()
return string(b)
}
// check "/" endpoint
res, err := s.Client().Get(s.URL)
if err != nil {
t.Fatal(err)
}
got := read(res)
for _, want := range []string{"Gaby", "meta", "flags", "log level"} {
if !strings.Contains(got, want) {
t.Errorf("response for '/' endpoint expected to contain %s; got %s", want, got)
}
}
// check "/setlevel" endpoint
_, err = s.Client().Get(fmt.Sprintf("%s/setlevel?l=error", s.URL))
if err != nil {
t.Fatal(err)
}
}