summaryrefslogtreecommitdiff
path: root/js_lib_test.go
diff options
context:
space:
mode:
authorPhilipp Tanlak <philipp.tanlak@gmail.com>2023-11-01 21:20:40 +0100
committerPhilipp Tanlak <philipp.tanlak@gmail.com>2023-11-01 21:20:40 +0100
commitaadd80b3b213988aa5701075f2650198e4066349 (patch)
tree23d61848106682a0c72338c07c37d6b9f87a1b47 /js_lib_test.go
parent2d3cd6584dedce45ea709d1757a28ce7537f3472 (diff)
Add login functionality
Diffstat (limited to 'js_lib_test.go')
-rw-r--r--js_lib_test.go91
1 files changed, 91 insertions, 0 deletions
diff --git a/js_lib_test.go b/js_lib_test.go
new file mode 100644
index 0000000..8d58c33
--- /dev/null
+++ b/js_lib_test.go
@@ -0,0 +1,91 @@
+// 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 flyscrape_test
+
+import (
+ "fmt"
+ "io"
+ "net/http"
+ "net/http/cookiejar"
+ "net/url"
+ "strings"
+ "testing"
+
+ "github.com/philippta/flyscrape"
+ "github.com/stretchr/testify/require"
+)
+
+func TestJSLibFetchDocument(t *testing.T) {
+ script := `
+ import { fetchDocument } from "flyscrape"
+
+ const doc = fetchDocument("https://example.com")
+ export const headline = doc.find("h1").text()
+ `
+
+ client := &http.Client{
+ Transport: flyscrape.MockTransport(200, html),
+ }
+
+ exports, err := flyscrape.Compile(script, flyscrape.NewJSLibrary(client))
+ require.NoError(t, err)
+
+ h, ok := exports["headline"].(string)
+ require.True(t, ok)
+ require.Equal(t, "headline", h)
+}
+
+func TestJSLibSubmitForm(t *testing.T) {
+ script := `
+ import { submitForm } from "flyscrape"
+
+ const doc = submitForm("https://example.com", {
+ "username": "foo",
+ "password": "bar",
+ })
+
+ export const text = doc.find("div").text()
+ `
+
+ var username, password string
+
+ jar, _ := cookiejar.New(nil)
+ client := &http.Client{
+ Jar: jar,
+ Transport: flyscrape.RoundTripFunc(func(r *http.Request) (*http.Response, error) {
+ username = r.FormValue("username")
+ password = r.FormValue("password")
+
+ resp := &http.Response{
+ StatusCode: 200,
+ Status: fmt.Sprintf("%d %s", 200, http.StatusText(200)),
+ Body: io.NopCloser(strings.NewReader(`<div>Login successful</div>`)),
+ Header: http.Header{},
+ }
+
+ cookie := http.Cookie{
+ Name: "example",
+ Value: "Hello world!",
+ Path: "/",
+ MaxAge: 3600,
+ }
+
+ resp.Header.Add("Set-Cookie", cookie.String())
+ return resp, nil
+ }),
+ }
+
+ exports, err := flyscrape.Compile(script, flyscrape.NewJSLibrary(client))
+ require.NoError(t, err)
+
+ text, ok := exports["text"].(string)
+ require.True(t, ok)
+ require.Equal(t, "Login successful", text)
+ require.Equal(t, "foo", username)
+ require.Equal(t, "bar", password)
+
+ u, _ := url.Parse("https://example.com")
+ require.NotEmpty(t, jar.Cookies(u))
+}