|
@@ -0,0 +1,78 @@
|
|
|
+package main_test
|
|
|
+
|
|
|
+/*
|
|
|
+ Reverse proxy chunked encoding test
|
|
|
+
|
|
|
+ To use this test file, set default site in your zoraxypointing to http://localhost:8088
|
|
|
+ and then start the backend server and test script below.
|
|
|
+*/
|
|
|
+
|
|
|
+import (
|
|
|
+ "bytes"
|
|
|
+ "fmt"
|
|
|
+ "io"
|
|
|
+ "io/ioutil"
|
|
|
+ "net/http"
|
|
|
+ "testing"
|
|
|
+)
|
|
|
+
|
|
|
+const (
|
|
|
+ backendResponse = "I am the backend"
|
|
|
+ backendStatus = 404
|
|
|
+ backendURL string = "http://localhost"
|
|
|
+)
|
|
|
+
|
|
|
+func TestReverseProxy(t *testing.T) {
|
|
|
+ getReq, _ := http.NewRequest("GET", backendURL, nil)
|
|
|
+ getReq.Host = "localhost"
|
|
|
+ res, err := http.DefaultClient.Do(getReq)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatalf("Get: %v", err)
|
|
|
+ }
|
|
|
+ if g, e := res.StatusCode, backendStatus; g != e {
|
|
|
+ t.Errorf("got res.StatusCode %d; expected %d", g, e)
|
|
|
+ }
|
|
|
+ if g, e := res.Header.Get("X-Foo"), "bar"; g != e {
|
|
|
+ t.Errorf("got X-Foo %q; expected %q", g, e)
|
|
|
+ }
|
|
|
+ if g, e := len(res.Header["Set-Cookie"]), 1; g != e {
|
|
|
+ t.Fatalf("got %d SetCookies, want %d", g, e)
|
|
|
+ }
|
|
|
+ if cookie := res.Cookies()[0]; cookie.Name != "flavor" {
|
|
|
+ t.Errorf("unexpected cookie %q", cookie.Name)
|
|
|
+ }
|
|
|
+ bodyBytes, _ := ioutil.ReadAll(res.Body)
|
|
|
+ if g, e := string(bodyBytes), backendResponse; g != e {
|
|
|
+ t.Errorf("got body %q; expected %q", g, e)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TestChunkedTransfer(t *testing.T) {
|
|
|
+ // Test chunked encoding request
|
|
|
+ chunkedReq, _ := http.NewRequest("POST", backendURL, bytes.NewBufferString(""))
|
|
|
+ chunkedReq.Host = "localhost"
|
|
|
+ chunkedReq.TransferEncoding = []string{"chunked"}
|
|
|
+ chunkedRes, err := http.DefaultClient.Do(chunkedReq)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatalf("Chunked POST: %v", err)
|
|
|
+ }
|
|
|
+ if g, e := chunkedRes.StatusCode, 200; g != e {
|
|
|
+ t.Errorf("got chunkedRes.StatusCode %d; expected %d", g, e)
|
|
|
+ }
|
|
|
+ // Read the response body in chunks and print to STDOUT
|
|
|
+ buf := make([]byte, 1024)
|
|
|
+ for {
|
|
|
+ n, err := chunkedRes.Body.Read(buf)
|
|
|
+ if n > 0 {
|
|
|
+ // Print the chunk to STDOUT
|
|
|
+ fmt.Print(string(buf[:n]))
|
|
|
+ }
|
|
|
+ if err != nil {
|
|
|
+ if err != io.EOF {
|
|
|
+ t.Fatalf("Error reading response body: %v", err)
|
|
|
+ }
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ chunkedRes.Body.Close()
|
|
|
+}
|