summaryrefslogtreecommitdiff
path: root/service/http/server_test.go
blob: 07c5bbdcedcb2255a7e7a88840672f051794287a (plain)
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
package http

import (
	"testing"
	"github.com/spiral/roadrunner"
	"os"
	"github.com/stretchr/testify/assert"
	"net/http"
	"context"
	"bytes"
	"io"
)

// get request and return body
func get(url string) (string, *http.Response, error) {
	r, err := http.Get(url)
	if err != nil {
		return "", nil, err
	}
	defer r.Body.Close()

	buf := new(bytes.Buffer)
	io.Copy(buf, r.Body)

	return buf.String(), r, nil
}

func TestServer_Echo(t *testing.T) {
	st := &Server{
		cfg: &Config{
			MaxRequest: 1024,
			Uploads: &UploadsConfig{
				Dir:    os.TempDir(),
				Forbid: []string{},
			},
		},
		rr: roadrunner.NewServer(&roadrunner.ServerConfig{
			Command: "php ../../php-src/tests/http/client.php echo pipes",
			Relay:   "pipes",
			Pool: &roadrunner.Config{
				NumWorkers:      1,
				AllocateTimeout: 10000000,
				DestroyTimeout:  10000000,
			},
		}),
	}

	assert.NoError(t, st.rr.Start())
	defer st.rr.Stop()

	hs := &http.Server{Addr: ":8077", Handler: st,}
	defer hs.Shutdown(context.Background())

	go func() { hs.ListenAndServe() }()

	body, r, err := get("http://localhost:8077/?hello=world")
	assert.NoError(t, err)
	assert.Equal(t, 200, r.StatusCode)
	assert.Equal(t, "WORLD", body)
}

func TestServer_EchoHeaders(t *testing.T) {
	st := &Server{
		cfg: &Config{
			MaxRequest: 1024,
			Uploads: &UploadsConfig{
				Dir:    os.TempDir(),
				Forbid: []string{},
			},
		},
		rr: roadrunner.NewServer(&roadrunner.ServerConfig{
			Command: "php ../../php-src/tests/http/client.php header pipes",
			Relay:   "pipes",
			Pool: &roadrunner.Config{
				NumWorkers:      1,
				AllocateTimeout: 10000000,
				DestroyTimeout:  10000000,
			},
		}),
	}

	assert.NoError(t, st.rr.Start())
	defer st.rr.Stop()

	hs := &http.Server{Addr: ":8077", Handler: st,}
	defer hs.Shutdown(context.Background())

	go func() { hs.ListenAndServe() }()

	_, r, err := get("http://localhost:8077/?hello=world")
	assert.NoError(t, err)
	assert.Equal(t, 200, r.StatusCode)
	assert.Equal(t, "world", r.Header.Get("Header"))
}