Files
telemt-api/internal/proxy/reverse_test.go
T

48 lines
1.2 KiB
Go

package proxy
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
func TestReverseProxyPathRewrite(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/health" {
t.Fatalf("path %q", r.URL.Path)
}
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
up, _ := url.Parse(srv.URL)
rp := NewReverseProxy(up, "/api/main_srv", "/v1", "")
req := httptest.NewRequest(http.MethodGet, "/api/main_srv/health", nil)
rec := httptest.NewRecorder()
rp.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status %d", rec.Code)
}
}
func TestReverseProxyPathRewriteWithAPIBasePath(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/health" {
t.Fatalf("path %q", r.URL.Path)
}
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
up, err := url.Parse(srv.URL + "/api/")
if err != nil {
t.Fatal(err)
}
rp := NewReverseProxy(up, "/api/main_srv", "/v1", "")
req := httptest.NewRequest(http.MethodGet, "/api/main_srv/health", nil)
rec := httptest.NewRecorder()
rp.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status %d", rec.Code)
}
}