34 lines
984 B
Go
34 lines
984 B
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"testing"
|
|
)
|
|
|
|
func TestAPIProxyPreservesMethodPathAndStatus(t *testing.T) {
|
|
requests := make(chan *http.Request, 1)
|
|
gateway := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
|
|
requests <- request
|
|
response.WriteHeader(http.StatusServiceUnavailable)
|
|
}))
|
|
defer gateway.Close()
|
|
|
|
upstream, err := url.Parse(gateway.URL)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
request := httptest.NewRequest(http.MethodPost, "/api/browsers/account-a/start", nil)
|
|
response := httptest.NewRecorder()
|
|
newHandler(upstream, t.TempDir()).ServeHTTP(response, request)
|
|
|
|
proxied := <-requests
|
|
if proxied.Method != http.MethodPost || proxied.URL.Path != "/v1/browsers/account-a/start" {
|
|
t.Fatalf("unexpected proxy request %s %s", proxied.Method, proxied.URL.Path)
|
|
}
|
|
if response.Code != http.StatusServiceUnavailable {
|
|
t.Fatalf("expected upstream status 503, got %d", response.Code)
|
|
}
|
|
}
|