summaryrefslogtreecommitdiff
path: root/modules/proxy/proxy_test.go
diff options
context:
space:
mode:
authorPhilipp Tanlak <philipp.tanlak@gmail.com>2023-10-17 19:19:38 +0200
committerPhilipp Tanlak <philipp.tanlak@gmail.com>2023-10-17 19:19:38 +0200
commit03b3be0c3bbc70584e8988e1810dc28eacf4521f (patch)
tree8eb1071aec0815b1cc8d34a4482907455ae5e8bd /modules/proxy/proxy_test.go
parent11d73f57a80bb65b7507ec80433b8f035ed226c2 (diff)
Add HTTP(S) Proxy support
Diffstat (limited to 'modules/proxy/proxy_test.go')
-rw-r--r--modules/proxy/proxy_test.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/modules/proxy/proxy_test.go b/modules/proxy/proxy_test.go
new file mode 100644
index 0000000..e6058b8
--- /dev/null
+++ b/modules/proxy/proxy_test.go
@@ -0,0 +1,62 @@
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+package proxy_test
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "testing"
+
+ "github.com/philippta/flyscrape"
+ "github.com/philippta/flyscrape/modules/proxy"
+ "github.com/philippta/flyscrape/modules/starturl"
+ "github.com/stretchr/testify/require"
+)
+
+func TestProxy(t *testing.T) {
+ var called bool
+ p := newProxy(func() { called = true })
+ defer p.Close()
+
+ scraper := flyscrape.NewScraper()
+ scraper.LoadModule(&starturl.Module{URL: "http://www.example.com"})
+ scraper.LoadModule(&proxy.Module{
+ Proxies: []string{p.URL},
+ })
+
+ scraper.Run()
+ require.True(t, called)
+}
+
+func TestProxyMultiple(t *testing.T) {
+ calls := []int{0, 0}
+ p0 := newProxy(func() { calls[0]++ })
+ p1 := newProxy(func() { calls[1]++ })
+ defer p0.Close()
+ defer p1.Close()
+
+ mod := &proxy.Module{Proxies: []string{p0.URL, p1.URL}}
+ mod.Provision(nil)
+ trans := mod.AdaptTransport(nil)
+
+ req := httptest.NewRequest("GET", "http://www.example.com/", nil)
+
+ for i := 0; i < 10; i++ {
+ resp, err := trans.RoundTrip(req)
+ require.NoError(t, err)
+ require.Equal(t, http.StatusOK, resp.StatusCode)
+ }
+
+ require.Greater(t, calls[0], 1)
+ require.Greater(t, calls[1], 1)
+ require.Equal(t, 10, calls[0]+calls[1])
+}
+
+func newProxy(f func()) *httptest.Server {
+ return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ f()
+ w.Write([]byte("response from proxy"))
+ }))
+}