From f5e56283272f8b7b1687c7cf30b6dbddb31ada4b Mon Sep 17 00:00:00 2001 From: Viacheslav Biriukov Date: Sun, 7 Jun 2015 20:36:28 +0000 Subject: [PATCH] add test --- middleware/sed/data.gz | Bin 0 -> 60 bytes middleware/sed/sed.go | 9 +- middleware/sed/sed_test.go | 185 +++++++++++++++++++++++++++++++++++++ 3 files changed, 191 insertions(+), 3 deletions(-) create mode 100644 middleware/sed/data.gz create mode 100644 middleware/sed/sed_test.go diff --git a/middleware/sed/data.gz b/middleware/sed/data.gz new file mode 100644 index 0000000000000000000000000000000000000000..4bc14846d0b31f132a1d3a119a1f88b500575a1a GIT binary patch literal 60 zcmV-C0K@+uiwFQ%p>$OM152;UEKtZODJihh*H5iT%q_@C)l1ILMHk9TEl~(bEiO?g SE-A{)O9uc7Vt>ju0002HIu)D% literal 0 HcmV?d00001 diff --git a/middleware/sed/sed.go b/middleware/sed/sed.go index df821aacd..37e46f855 100644 --- a/middleware/sed/sed.go +++ b/middleware/sed/sed.go @@ -14,6 +14,11 @@ import ( "github.com/mholt/caddy/middleware" ) +var ( + cts = []string{"text/html"} // TODO (brk0v): only html pages + size = 1 << 18 // default 256 KB +) + // bufferedWriter buffers responce for replacing content. type bufferedWriter struct { http.ResponseWriter @@ -130,7 +135,7 @@ func (bw *bufferedWriter) Apply(r *http.Request) ([]byte, error) { var body []byte if bw.ContentEncoding != "" { if r.Header.Get("Accept-Encoding") == "" { - // gzip middleware has been already ungziped data + // gzip middleware has been already uncompressed data body = bw.Body.Bytes() return body, nil } @@ -178,8 +183,6 @@ func (s Sed) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { if middleware.Path(r.URL.Path).Matches(rule.Url) { // Buffering write. - cts := []string{"text/html"} // TODO (brk0v): only html pages - size := 1 << 18 // default 256 KB if rule.Size != 0 { size = rule.Size } diff --git a/middleware/sed/sed_test.go b/middleware/sed/sed_test.go new file mode 100644 index 000000000..d83a7365f --- /dev/null +++ b/middleware/sed/sed_test.go @@ -0,0 +1,185 @@ +package sed + +import ( + "bytes" + "io/ioutil" + "net/http" + "net/http/httptest" + "testing" + + "github.com/mholt/caddy/middleware" +) + +func TestSedpHandler(t *testing.T) { + // Config Sed middleware + patterns := []Pattern{ + Pattern{ + Find: "may", + Replace: "must", + }, + Pattern{ + Find: "This", + Replace: "exmaple.com", + }, + Pattern{ + Find: "http://", + Replace: "https://", + }, + } + sed := Sed{ + Rules: []Rule{ + Rule{ + Patterns: patterns, + Url: "/", + }, + }, + } + + // Load gziped data. + gzData, err := ioutil.ReadFile("data.gz") + if err != nil { + t.Fatal(err) + } + + // Test with disabled gzip module. While it's turned off we can get + // Content-Encoding header from proxy backend. We trust it. + var tests = []struct { + body []byte // body test + expected []byte // expected body + ctt string // Content-Type for response + cte string // Contetn-Encoding for response + ae string // Accept-Encoding for request + }{ + { + body: []byte("This domain is established to be used for illustrative examples in documents. You may use this domain in examples without prior coordination or asking for permission."), + expected: []byte("exmaple.com domain is established to be used for illustrative examples in documents. You must use this domain in examples without prior coordination or asking for permission."), + ctt: "text/html; utf-8", + cte: "", + ae: "gzip", + }, + { + body: []byte("This domain is established to be used for illustrative examples in documents. You may use this domain in examples without prior coordination or asking for permission."), + expected: []byte("This domain is established to be used for illustrative examples in documents. You may use this domain in examples without prior coordination or asking for permission."), + ctt: "", + cte: "", + ae: "gzip", + }, + { + body: []byte(""), + expected: []byte(""), + ctt: "", + cte: "", + ae: "gzip", + }, + { + body: []byte(""), + expected: []byte(""), + ctt: "text/html; utf-8", + cte: "", + ae: "gzip", + }, + { + body: []byte("http://example.com http://example.net Test string"), + expected: []byte("https://example.com https://example.net Test string"), + ctt: "text/html; utf-8", + cte: "", + ae: "gzip", + }, + { + body: []byte("http://example.com http://example.net Test string"), + expected: []byte("http://example.com http://example.net Test string"), + ctt: "", + cte: "", + ae: "gzip", + }, + { + body: gzData, + expected: []byte("gzip https://example.com https://example.net Test string"), + ctt: "text/html; utf-8", + cte: "gzip", + ae: "gzip", + }, + { + body: gzData, + expected: gzData, + ctt: "", + cte: "gzip", + ae: "gzip", + }, + { + body: gzData, + expected: gzData, + ctt: "text/html; utf-8", + cte: "", + ae: "gzip", + }, + { + body: gzData, + expected: gzData, + ctt: "", + cte: "", + ae: "gzip", + }, + { + body: []byte("gzip http://example.com http://example.net Test string"), + expected: []byte("gzip https://example.com https://example.net Test string"), + ctt: "text/html; utf-8", + cte: "gzip", + ae: "", + }, + { + body: []byte("gzip http://example.com http://example.net Test string"), + expected: []byte("gzip http://example.com http://example.net Test string"), + ctt: "", + cte: "gzip", + ae: "", + }, + { + body: []byte("gzip http://example.com http://example.net Test string"), + expected: []byte("gzip https://example.com https://example.net Test string"), + ctt: "text/html; utf-8", + cte: "", + ae: "", + }, + { + body: []byte("gzip http://example.com http://example.net Test string"), + expected: []byte("gzip http://example.com http://example.net Test string"), + ctt: "", + cte: "", + ae: "", + }, + } + + for _, tt := range tests { + w := httptest.NewRecorder() + sed.Next = nextFunc(tt.body, tt.ctt, tt.cte) + r, err := http.NewRequest("GET", "/", nil) + if tt.ae != "" { + r.Header.Set("Accept-Encoding", tt.ae) + } + if err != nil { + t.Error(err) + } + _, err = sed.ServeHTTP(w, r) + if err != nil { + t.Error(err) + } + + if !bytes.Equal(w.Body.Bytes(), tt.expected) { + t.Errorf("Expected '%s' but got '%s' instead", tt.expected, w.Body.Bytes()) + } + } +} + +func nextFunc(body []byte, ctt, cte string) middleware.Handler { + return middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { + if ctt != "" { + w.Header().Set("Content-Type", ctt) + } + if cte != "" { + w.Header().Set("Content-Encoding", cte) + } + w.Write(body) + return 0, nil + }) +}