-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
48 lines (46 loc) · 1.21 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
package main
import (
"context"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"io"
"net/http"
"strings"
"testing"
)
func TestExample(t *testing.T) {
containerCtx := context.Background()
chuckNorris, _ := testcontainers.GenericContainer(containerCtx, testcontainers.GenericContainerRequest{
Started: true,
ContainerRequest: testcontainers.ContainerRequest{
Image: "marcnuri/chuck-norris:latest",
ExposedPorts: []string{"8080/tcp"},
WaitingFor: wait.ForLog("Listening on: http://0.0.0.0:8080"),
},
})
defer func() {
err := chuckNorris.Terminate(containerCtx)
if err != nil {
t.Error(err)
}
}()
endpoint, err := chuckNorris.Endpoint(containerCtx, "http")
if err != nil {
t.Error(err)
}
response, err := http.Get(endpoint)
if err != nil {
t.Error(err)
}
if response.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, got %d", http.StatusOK, response.StatusCode)
}
body, err := io.ReadAll(response.Body)
if err != nil {
t.Error(err)
}
bodyString := string(body)
if !strings.Contains(strings.ToLower(bodyString), "chuck") {
t.Errorf("Expected a Chuck Norris approved response, got \"%s\"", bodyString)
}
}