-
Notifications
You must be signed in to change notification settings - Fork 12
/
server_test.go
57 lines (49 loc) · 1.74 KB
/
server_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
package main
import (
"github.com/cskr/pubsub"
"github.com/spf13/viper"
"io"
"net/http"
"testing"
"time"
)
// TestStartWebServer is a unit test for StartWebServer
//
// It loads up the default config and creates a PubSub broker
// It then confirms the webserver is responding, to both / and api calls
func TestStartWebServer(t *testing.T) {
// Load in the default config file, so we get some regex patterns
HandleConfigFile("logstation.default.conf")
// Setup message broker
pubSub := pubsub.New(1)
go StartWebServer(pubSub)
// Give the StartWebServer enough time to startup
time.Sleep(time.Duration(1000) * time.Millisecond)
// Server should be accessible at localhost
serverUrl := "http://127.0.0.1" + ":" + viper.GetString("server_settings.webserverport")
// See if the webserver is up and responding at /
response, err := http.Get(serverUrl)
if err != nil {
t.Errorf("Unable to request the webserver at %s", serverUrl)
}
if response.StatusCode != 200 {
t.Errorf("Server responded with a bad status code: %s", response.Status)
}
// Verify it responds to /settings/logstation-name
response, err = http.Get(serverUrl + "/settings/logstation-name")
if err != nil {
t.Errorf("Unable to request the webserver at %s", serverUrl+"/settings/logstation-name")
}
if response.StatusCode != 200 {
t.Errorf("Server responded with a bad status code: %s", response.Status)
}
responseData, err := io.ReadAll(response.Body)
if err != nil {
t.Errorf("Failed to read the body, err: %s", err)
}
expectedResponse := "{\"name\":\"logstation\"}\n"
responseString := string(responseData)
if responseString != expectedResponse {
t.Errorf("Response for /settings/logstation-name was expected to be %s, but got %s", expectedResponse, responseData)
}
}