|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +func TestPrepareFoo(t *testing.T) { |
| 13 | + http.HandleFunc("/foo/", func(w http.ResponseWriter, r *http.Request) { |
| 14 | + fmt.Fprint(w, "hello Foo\n") |
| 15 | + }) |
| 16 | + go func() { |
| 17 | + err := http.ListenAndServe(":10001", nil) |
| 18 | + if err != nil { |
| 19 | + t.Error(err) |
| 20 | + } |
| 21 | + }() |
| 22 | + time.Sleep(1 * time.Second) |
| 23 | +} |
| 24 | + |
| 25 | +func TestPrepareBar(t *testing.T) { |
| 26 | + http.HandleFunc("/bar/", func(w http.ResponseWriter, r *http.Request) { |
| 27 | + fmt.Fprint(w, "hello Bar\n") |
| 28 | + }) |
| 29 | + go func() { |
| 30 | + err := http.ListenAndServe(":10002", nil) |
| 31 | + if err != nil { |
| 32 | + t.Error(err) |
| 33 | + } |
| 34 | + }() |
| 35 | + time.Sleep(1 * time.Second) |
| 36 | +} |
| 37 | + |
| 38 | +func TestRunHTTPd(t *testing.T) { |
| 39 | + f, err := ioutil.TempFile("", "") |
| 40 | + if err != nil { |
| 41 | + t.Error(err) |
| 42 | + } |
| 43 | + defer func() { |
| 44 | + f.Close() |
| 45 | + os.Remove(f.Name()) |
| 46 | + }() |
| 47 | + data := ` |
| 48 | +address: "127.0.0.1:9999" |
| 49 | +auth: |
| 50 | + session: |
| 51 | + key: dummy |
| 52 | + info: |
| 53 | + service: nothing |
| 54 | + client_id: dummy |
| 55 | + client_secret: dummy |
| 56 | + redirect_url: "http://example.com/oauth2callback" |
| 57 | +proxy: |
| 58 | + - path: /foo |
| 59 | + dest: http://127.0.0.1:10001 |
| 60 | + strip_path: no |
| 61 | +
|
| 62 | + - path: /bar |
| 63 | + dest: http://127.0.0.1:10002 |
| 64 | + strip_path: no |
| 65 | +` |
| 66 | + if err := ioutil.WriteFile(f.Name(), []byte(data), 0644); err != nil { |
| 67 | + t.Error(err) |
| 68 | + } |
| 69 | + conf, err := ParseConf(f.Name()) |
| 70 | + if err != nil { |
| 71 | + t.Error(err) |
| 72 | + } |
| 73 | + server := NewServer(conf) |
| 74 | + if server == nil { |
| 75 | + t.Error("NewServer failed") |
| 76 | + } |
| 77 | + go server.Run() |
| 78 | + time.Sleep(1 * time.Second) |
| 79 | + |
| 80 | + // backend foo |
| 81 | + if res, err := http.Get("http://127.0.0.1:9999/foo/"); err == nil { |
| 82 | + defer res.Body.Close() |
| 83 | + body, _ := ioutil.ReadAll(res.Body) |
| 84 | + if string(body) != "hello Foo\n" { |
| 85 | + t.Errorf("unexpected foo body %s", body) |
| 86 | + } |
| 87 | + } else { |
| 88 | + t.Error(err) |
| 89 | + } |
| 90 | + |
| 91 | + // backend bar |
| 92 | + if res, err := http.Get("http://127.0.0.1:9999/bar/"); err == nil { |
| 93 | + defer res.Body.Close() |
| 94 | + body, _ := ioutil.ReadAll(res.Body) |
| 95 | + if string(body) != "hello Bar\n" { |
| 96 | + t.Errorf("unexpected bar body %s", body) |
| 97 | + } |
| 98 | + } else { |
| 99 | + t.Error(err) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func TestRunVhost(t *testing.T) { |
| 104 | + f, err := ioutil.TempFile("", "") |
| 105 | + if err != nil { |
| 106 | + t.Error(err) |
| 107 | + } |
| 108 | + defer func() { |
| 109 | + f.Close() |
| 110 | + os.Remove(f.Name()) |
| 111 | + }() |
| 112 | + data := ` |
| 113 | +address: "127.0.0.1:10000" |
| 114 | +auth: |
| 115 | + session: |
| 116 | + key: dummy |
| 117 | + cookie_domain: example.com |
| 118 | + info: |
| 119 | + service: nothing |
| 120 | + client_id: dummy |
| 121 | + client_secret: dummy |
| 122 | + redirect_url: "http://example.com/oauth2callback" |
| 123 | +proxy: |
| 124 | + - path: / |
| 125 | + dest: http://127.0.0.1:10001 |
| 126 | + strip_path: no |
| 127 | + host: foo.example.com |
| 128 | +
|
| 129 | + - path: / |
| 130 | + dest: http://127.0.0.1:10002 |
| 131 | + strip_path: no |
| 132 | + host: bar.example.com |
| 133 | +` |
| 134 | + if err := ioutil.WriteFile(f.Name(), []byte(data), 0644); err != nil { |
| 135 | + t.Error(err) |
| 136 | + } |
| 137 | + conf, err := ParseConf(f.Name()) |
| 138 | + if err != nil { |
| 139 | + t.Error(err) |
| 140 | + } |
| 141 | + server := NewServer(conf) |
| 142 | + if server == nil { |
| 143 | + t.Error("NewServer failed") |
| 144 | + } |
| 145 | + go server.Run() |
| 146 | + time.Sleep(1 * time.Second) |
| 147 | + |
| 148 | + var req *http.Request |
| 149 | + client := &http.Client{} |
| 150 | + |
| 151 | + // backend foo |
| 152 | + req, _ = http.NewRequest("GET", "http://127.0.0.1:10000/foo/", nil) |
| 153 | + req.Header.Add("Host", "foo.example.com") |
| 154 | + if res, err := client.Do(req); err == nil { |
| 155 | + defer res.Body.Close() |
| 156 | + body, _ := ioutil.ReadAll(res.Body) |
| 157 | + if string(body) != "hello Foo\n" { |
| 158 | + t.Errorf("unexpected foo body %s", body) |
| 159 | + } |
| 160 | + } else { |
| 161 | + t.Error(err) |
| 162 | + } |
| 163 | + |
| 164 | + // backend bar |
| 165 | + req, _ = http.NewRequest("GET", "http://127.0.0.1:10000/bar/", nil) |
| 166 | + req.Header.Add("Host", "bar.example.com") |
| 167 | + if res, err := http.Get("http://127.0.0.1:10000/bar/"); err == nil { |
| 168 | + defer res.Body.Close() |
| 169 | + body, _ := ioutil.ReadAll(res.Body) |
| 170 | + if string(body) != "hello Bar\n" { |
| 171 | + t.Errorf("unexpected bar body %s", body) |
| 172 | + } |
| 173 | + } else { |
| 174 | + t.Error(err) |
| 175 | + } |
| 176 | + |
| 177 | +} |
0 commit comments