plaprox

Proxy for plausible.io scripts written in Go.
git clone git://vcs.sapka.me/plaprox
Log | Files | Refs | README | LICENSE

commit 52a2ba0fdeb3677dd9675afce2b8be84d8a31d48
parent 7ec127eb314d617d74393c6efb846015d39e09c8
Author: MichaƂ M. Sapka <michal@sapka.me>
Date:   Fri, 14 Jul 2023 13:56:09 +0200

fix: gitgnore after name change

Diffstat:
M.gitignore | 2+-
Mgo.mod | 7+++++--
Mgo.sum | 4++++
Mmain.go | 20+++++++++-----------
Mmain_test.go | 88+++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
5 files changed, 83 insertions(+), 38 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1 +1 @@ -proxy +plaprox diff --git a/go.mod b/go.mod @@ -1,10 +1,13 @@ module vcs.sapka.me/plaprox - go 1.20 require ( + github.com/jarcoal/httpmock v1.3.0 + gopkg.in/sevlyar/go-daemon.v0 v0.1.6 +) + +require ( github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect golang.org/x/sys v0.10.0 // indirect - gopkg.in/sevlyar/go-daemon.v0 v0.1.6 // indirect ) diff --git a/go.sum b/go.sum @@ -1,5 +1,9 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/jarcoal/httpmock v1.3.0 h1:2RJ8GP0IIaWwcC9Fp2BmVi8Kog3v2Hn7VXM3fTd+nuc= +github.com/jarcoal/httpmock v1.3.0/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg= github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 h1:iQTw/8FWTuc7uiaSepXwyf3o52HaUYcV+Tu66S3F5GA= github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8= +github.com/maxatome/go-testdeep v1.12.0 h1:Ql7Go8Tg0C1D/uMMX59LAoYK7LffeJQ6X2T04nTH68g= golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/sevlyar/go-daemon.v0 v0.1.6 h1:qy+vJZ3kXgz582jjwiYgMysXhT8Un90GvZqDpaSkOLs= diff --git a/main.go b/main.go @@ -39,8 +39,6 @@ func init() { Client = &http.Client{} } -const PlausibleHost = "plausible.io" - func main() { serveProxy() @@ -70,17 +68,17 @@ func main() { // Proxies requests to Plausible.io and returns the response to the client func Proxy(w http.ResponseWriter, r *http.Request) { - req, _ := http.NewRequest(r.Method, "https://"+PlausibleHost+r.URL.Path, r.Body) - + req, _ := http.NewRequest(r.Method, "https://plausible.io"+r.URL.Path, r.Body) + /* - Forwarding headers breaks reading the returned file in browsers. Seems to be - not needed, so I'll just comment this logic out - for key, values := range r.Header { - req.Header.Del(key) - for _, value := range values { - req.Header.Add(key, value) + Forwarding headers breaks reading the returned file in browsers. Seems to be + not needed, so I'll just comment this logic out + for key, values := range r.Header { + req.Header.Del(key) + for _, value := range values { + req.Header.Add(key, value) + } } - } */ resp, _ := Client.Do(req) defer resp.Body.Close() diff --git a/main_test.go b/main_test.go @@ -7,6 +7,8 @@ import ( "io/ioutil" "net/http" "net/http/httptest" + // "net/http/httputil" + "testing" ) @@ -19,7 +21,7 @@ func (m *MockClient) Do(req *http.Request) (*http.Response, error) { return m.MockDo(req) } -func mockProxiedCall(url string, responseBody string, headers map[string][]string, t *testing.T) { +func mockProxiedCall(method string, url string, responseBody string, sentBody string, headers map[string][]string, t *testing.T) { r := ioutil.NopCloser(bytes.NewReader([]byte(responseBody))) Client = &MockClient{ @@ -31,10 +33,25 @@ func mockProxiedCall(url string, responseBody string, headers map[string][]strin if req.URL.Path != url { t.Errorf("Invalid url. Got: %q, want: %q", req.URL.Path, url) } - /* - if fmt.Sprint(req.Header) != fmt.Sprint(headers) { - t.Errorf("Invalid proxied headers. Got: %q, want: %q", req.Header, headers) + if req.Method != method { + t.Errorf("Invalid method. Got: %q, want: %q", req.Method, method) } + + // if req.Method == "POST" { + // req.ParseForm() + // responseBody2 := req.Form + + // fmt.Println("inm test", req.Body) + + // jo, _ := httputil.DumpRequest(req, true) + // fmt.Println("fuck", string(jo[:])) + + // } + + /* + if fmt.Sprint(req.Header) != fmt.Sprint(headers) { + t.Errorf("Invalid proxied headers. Got: %q, want: %q", req.Header, headers) + } */ return &http.Response{ @@ -49,31 +66,54 @@ func mockProxiedCall(url string, responseBody string, headers map[string][]strin } func TestProxy(t *testing.T) { - proxiedBody := `[{"full_name": "mock-repo" }]` - wantedHeaders := map[string][]string{ - "Key": []string{"value"}, - "Key2": []string{"value1", "value2"}, - "Content-Type": []string{"application/javascript"}, - } + t.Run("GET request", func(t *testing.T) { + proxiedBody := `[{"full_name": "mock-repo" }]` + sentBody := "{}" + wantedHeaders := map[string][]string{ + "Key": []string{"value"}, + "Key2": []string{"value1", "value2"}, + "Content-Type": []string{"application/javascript"}, + } - mockProxiedCall("/js/script.js", proxiedBody, wantedHeaders, t) + mockProxiedCall(http.MethodGet, "/js/script.js", proxiedBody, sentBody, wantedHeaders, t) - request, _ := http.NewRequest(http.MethodGet, "/js/script.js", nil) + request, _ := http.NewRequest(http.MethodGet, "/js/script.js", nil) - request.Header.Add("key", "value") - request.Header.Add("key2", "value1") - request.Header.Add("key2", "value2") - request.Header.Set("Content-Type", "application/javascript") + request.Header.Add("key", "value") + request.Header.Add("key2", "value1") + request.Header.Add("key2", "value2") + request.Header.Set("Content-Type", "application/javascript") - response := httptest.NewRecorder() + response := httptest.NewRecorder() - Proxy(response, request) + Proxy(response, request) - if response.Body.String() != proxiedBody { - t.Errorf("got %q, want %q", response.Body.String(), proxiedBody) - } + if response.Body.String() != proxiedBody { + t.Errorf("got %q, want %q", response.Body.String(), proxiedBody) + } - if fmt.Sprint(response.Header()) != fmt.Sprint(wantedHeaders) { - t.Errorf("Invalid returned headers. Got: %q, want: %q", response.Header(), wantedHeaders) - } + if fmt.Sprint(response.Header()) != fmt.Sprint(wantedHeaders) { + t.Errorf("Invalid returned headers. Got: %q, want: %q", response.Header(), wantedHeaders) + } + }) + + t.Run("POST request", func(t *testing.T) { + proxiedBody := "{}" + sentBody := `{"full_name": "mock-repo" }` + wantedHeaders := map[string][]string{} + + mockProxiedCall(http.MethodPost, "/js/script.js", proxiedBody, sentBody, wantedHeaders, t) + + request, _ := http.NewRequest( + http.MethodPost, + "/js/script.js", + bytes.NewReader([]byte(sentBody)), + ) + request.Header.Set("Content-Type", "application/javascript") + + response := httptest.NewRecorder() + + Proxy(response, request) + + }) }