summaryrefslogtreecommitdiff
path: root/service/http/h2c_test.go
blob: f17538bcc6f5838c0042b28b6d7aae5eb8976c30 (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
package http

import (
	"net/http"
	"testing"
	"time"

	"github.com/cenkalti/backoff/v4"
	"github.com/sirupsen/logrus"
	"github.com/sirupsen/logrus/hooks/test"
	"github.com/spiral/roadrunner/service"
	"github.com/stretchr/testify/assert"
)

func Test_Service_H2C(t *testing.T) {
	bkoff := backoff.NewExponentialBackOff()
	bkoff.MaxElapsedTime = time.Second * 15

	err := backoff.Retry(func() error {
		logger, _ := test.NewNullLogger()
		logger.SetLevel(logrus.DebugLevel)

		c := service.NewContainer(logger)
		c.Register(ID, &Service{})

		err := c.Init(&testCfg{httpCfg: `{
			"address": ":6029",
			"http2": {"h2c":true},
			"workers":{
				"command": "php ../../tests/http/client.php echo pipes",
				"relay": "pipes",
				"pool": {
					"numWorkers": 1
				}
			}
	}`})
		if err != nil {
			return err
		}

		s, st := c.Get(ID)
		assert.NotNil(t, s)
		assert.Equal(t, service.StatusOK, st)

		// should do nothing
		s.(*Service).Stop()

		go func() {
			err := c.Serve()
			if err != nil {
				t.Errorf("error serving: %v", err)
			}
		}()
		time.Sleep(time.Millisecond * 100)
		defer c.Stop()

		req, err := http.NewRequest("PRI", "http://localhost:6029?hello=world", nil)
		if err != nil {
			return err
		}

		req.Header.Add("Upgrade", "h2c")
		req.Header.Add("Connection", "HTTP2-Settings")
		req.Header.Add("HTTP2-Settings", "")

		r, err2 := http.DefaultClient.Do(req)
		if err2 != nil {
			return err2
		}

		assert.Equal(t, "101 Switching Protocols", r.Status)

		err3 := r.Body.Close()
		if err3 != nil {
			return err3
		}
		return nil
	}, bkoff)

	if err != nil {
		t.Fatal(err)
	}
}