summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2021-04-28 16:43:59 +0300
committerValery Piashchynski <[email protected]>2021-04-28 16:43:59 +0300
commitb789df7dcc9268f98f2bacfb40f753b10d521e4f (patch)
tree126b0fd8271f27e77f9107906bb0e6713a49d6ab /tests
parent30c25f17fa7d6386e33a4894c812f7ca5db990ad (diff)
- Update CHANGELOG
- Add tests for the etags functionality Signed-off-by: Valery Piashchynski <[email protected]>
Diffstat (limited to 'tests')
-rw-r--r--tests/plugins/http/configs/.rr-http-static-etags.yaml35
-rw-r--r--tests/plugins/http/configs/.rr-http-static.yaml6
-rw-r--r--tests/plugins/http/http_plugin_test.go98
3 files changed, 137 insertions, 2 deletions
diff --git a/tests/plugins/http/configs/.rr-http-static-etags.yaml b/tests/plugins/http/configs/.rr-http-static-etags.yaml
new file mode 100644
index 00000000..e18c50dd
--- /dev/null
+++ b/tests/plugins/http/configs/.rr-http-static-etags.yaml
@@ -0,0 +1,35 @@
+server:
+ command: "php ../../http/client.php pid pipes"
+ user: ""
+ group: ""
+ env:
+ "RR_HTTP": "true"
+ relay: "pipes"
+ relay_timeout: "20s"
+
+http:
+ address: 127.0.0.1:21603
+ max_request_size: 1024
+ middleware: [ "gzip" ]
+ trusted_subnets: [ "10.0.0.0/8", "127.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "::1/128", "fc00::/7", "fe80::/10" ]
+ uploads:
+ forbid: [ ".php", ".exe", ".bat" ]
+ static:
+ dir: "../../../"
+ pattern: "/tests/"
+ forbid: [ "" ]
+ allow: [ ".txt", ".php" ]
+ calculate_etag: true
+ weak: true
+ request:
+ "input": "custom-header"
+ response:
+ "output": "output-header"
+ pool:
+ num_workers: 2
+ max_jobs: 0
+ allocate_timeout: 60s
+ destroy_timeout: 60s
+logs:
+ mode: development
+ level: error
diff --git a/tests/plugins/http/configs/.rr-http-static.yaml b/tests/plugins/http/configs/.rr-http-static.yaml
index 9351f020..bbec13f9 100644
--- a/tests/plugins/http/configs/.rr-http-static.yaml
+++ b/tests/plugins/http/configs/.rr-http-static.yaml
@@ -1,5 +1,5 @@
server:
- command: "php ../../psr-worker-bench.php"
+ command: "php ../../http/client.php pid pipes"
user: ""
group: ""
env:
@@ -19,12 +19,14 @@ http:
pattern: "/tests/"
forbid: [ "" ]
allow: [ ".txt", ".php" ]
+ calculate_etag: true
+ weak: false
request:
"input": "custom-header"
response:
"output": "output-header"
pool:
- num_workers: 12
+ num_workers: 2
max_jobs: 0
allocate_timeout: 60s
destroy_timeout: 60s
diff --git a/tests/plugins/http/http_plugin_test.go b/tests/plugins/http/http_plugin_test.go
index 61486eef..f48194c9 100644
--- a/tests/plugins/http/http_plugin_test.go
+++ b/tests/plugins/http/http_plugin_test.go
@@ -11,6 +11,7 @@ import (
"net/http"
"net/http/httptest"
"net/rpc"
+ "net/url"
"os"
"os/signal"
"sync"
@@ -1562,6 +1563,103 @@ func bigEchoHTTP(t *testing.T) {
assert.NoError(t, err)
}
+func TestStaticEtagPlugin(t *testing.T) {
+ cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel))
+ assert.NoError(t, err)
+
+ cfg := &config.Viper{
+ Path: "configs/.rr-http-static.yaml",
+ Prefix: "rr",
+ }
+
+ err = cont.RegisterAll(
+ cfg,
+ &logger.ZapLogger{},
+ &server.Plugin{},
+ &httpPlugin.Plugin{},
+ &gzip.Plugin{},
+ )
+ assert.NoError(t, err)
+
+ err = cont.Init()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ ch, err := cont.Serve()
+ assert.NoError(t, err)
+
+ sig := make(chan os.Signal, 1)
+ signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
+
+ wg := &sync.WaitGroup{}
+ wg.Add(1)
+
+ stopCh := make(chan struct{}, 1)
+
+ go func() {
+ defer wg.Done()
+ for {
+ select {
+ case e := <-ch:
+ assert.Fail(t, "error", e.Error.Error())
+ err = cont.Stop()
+ if err != nil {
+ assert.FailNow(t, "error", err.Error())
+ }
+ case <-sig:
+ err = cont.Stop()
+ if err != nil {
+ assert.FailNow(t, "error", err.Error())
+ }
+ return
+ case <-stopCh:
+ // timeout
+ err = cont.Stop()
+ if err != nil {
+ assert.FailNow(t, "error", err.Error())
+ }
+ return
+ }
+ }
+ }()
+
+ time.Sleep(time.Second)
+ t.Run("ServeSampleEtag", serveStaticSampleEtag)
+
+ stopCh <- struct{}{}
+ wg.Wait()
+}
+
+func serveStaticSampleEtag(t *testing.T) {
+ // OK 200 response
+ b, r, err := get("http://localhost:21603/tests/static/sample.txt")
+ assert.NoError(t, err)
+ assert.Equal(t, "sample\n", b)
+ assert.Equal(t, r.StatusCode, http.StatusOK)
+ etag := r.Header.Get("Etag")
+
+ _ = r.Body.Close()
+
+ // Should be 304 response with same etag
+ c := http.Client{
+ Timeout: time.Second * 5,
+ }
+
+ parsedURL, _ := url.Parse("http://localhost:21603/tests/static/sample.txt")
+
+ req := &http.Request{
+ Method: http.MethodGet,
+ URL: parsedURL,
+ Header: map[string][]string{"If-None-Match": {etag}},
+ }
+
+ resp, err := c.Do(req)
+ assert.Nil(t, err)
+ assert.Equal(t, http.StatusNotModified, resp.StatusCode)
+ _ = resp.Body.Close()
+}
+
func TestStaticPlugin(t *testing.T) {
cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel))
assert.NoError(t, err)