forked from localstack/lambda-runtime-init
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver_test.go
More file actions
117 lines (101 loc) · 2.84 KB
/
Copy pathserver_test.go
File metadata and controls
117 lines (101 loc) · 2.84 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package rapi
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"testing"
"time"
"go.amzn.com/lambda/testdata"
"github.com/stretchr/testify/assert"
)
const nextAvailablePort = 0 // net.Listener convention for next available port
const serverAddress = "127.0.0.1"
func createTestServer(handlerFunc http.HandlerFunc) (*Server, error) {
host, server := serverAddress, &http.Server{Handler: handlerFunc}
s := &Server{
host: host,
port: nextAvailablePort,
server: server,
listener: nil,
exit: make(chan error, 1),
}
err := s.Listen()
return s, err
}
func TestServerReturnsSuccessfulResponse(t *testing.T) {
expectedResponse := "foo"
testHandler := http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, expectedResponse)
})
s, err := createTestServer(testHandler)
if err != nil {
assert.FailNowf(t, "Server failed to listen", err.Error())
}
go func() {
s.Serve(context.Background())
}()
resp, err := http.Get(fmt.Sprintf("http://%s/", s.listener.Addr()))
if err != nil {
assert.FailNowf(t, "Failed to get response", err.Error())
}
body, err := io.ReadAll(resp.Body)
if err != nil {
assert.FailNowf(t, "Failed to read response body", err.Error())
}
resp.Body.Close()
s.Close()
assert.Equal(t, expectedResponse, string(body))
}
func TestServerExitsOnContextCancelation(t *testing.T) {
testHandler := http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "foo")
})
ctx, cancel := context.WithCancel(context.Background())
errChan := make(chan error, 1)
s, err := createTestServer(testHandler)
if err != nil {
assert.FailNowf(t, "Server failed to listen", err.Error())
}
go func() {
errChan <- s.Serve(ctx)
}()
pingRequestFunc := func() (bool, error) {
if _, err := http.Get(s.URL("/ping")); err != nil {
return false, err
}
return true, nil
}
serverStarted := testdata.Eventually(t, pingRequestFunc, 10*time.Millisecond, 10)
assert.True(t, serverStarted)
cancel()
error := testdata.WaitForErrorWithTimeout(errChan, 2*time.Second)
s.Close()
assert.Error(t, error)
assert.Contains(t, error.Error(), ctx.Err().Error())
}
func TestServerExitsOnExitSignalFromHandler(t *testing.T) {
testHandler := http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "foo")
})
errChan := make(chan error, 1)
s, err := createTestServer(testHandler)
if err != nil {
assert.FailNowf(t, "Server failed to listen", err.Error())
}
go func() {
errChan <- s.Serve(context.Background())
}()
exitError := errors.New("foo bar error")
s.exit <- exitError
error := testdata.WaitForErrorWithTimeout(errChan, time.Second)
s.Close()
assert.Error(t, error)
assert.Contains(t, error.Error(), exitError.Error())
}