Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added DOCUMENT_EXISTS function #594

Merged
merged 2 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions pkg/stdlib/html/document_exists.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package html

import (
"context"
"net/http"

"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
)

// DOCUMENT_EXISTS returns a boolean value indicating whether a web page exists by a given url.
// @param {String} url - Target url.
// @param {Object} [options] - Request options.
// @param {Object} [options.headers] - Request headers.
// @return {Boolean} - A boolean value indicating whether a web page exists by a given url.
func DocumentExists(ctx context.Context, args ...core.Value) (core.Value, error) {
if err := core.ValidateArgs(args, 1, 2); err != nil {
return nil, err
}

if err := core.ValidateType(args[0], types.String); err != nil {
return nil, err
}

url := args[0].String()

client := http.Client{}
req, err := http.NewRequest("GET", url, nil)

if err != nil {
return values.None, err
}

if len(args) > 1 {
if err := core.ValidateType(args[1], types.Object); err != nil {
return nil, err
}

options := args[1].(*values.Object)

if options.Has("headers") {
headersOpt := options.MustGet("headers")

if err := core.ValidateType(headersOpt, types.Object); err != nil {
return nil, err
}

headers := headersOpt.(*values.Object)

req.Header = http.Header{}

headers.ForEach(func(value core.Value, key string) bool {
req.Header.Set(key, value.String())

return true
})
}
}

resp, err := client.Do(req.WithContext(ctx))

if err != nil {
return values.False, nil
}

var exists values.Boolean

if resp.StatusCode >= 200 && resp.StatusCode <= 299 {
exists = values.True
}

return exists, nil
}
48 changes: 48 additions & 0 deletions pkg/stdlib/html/document_exists_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package html_test

import (
"context"
"testing"

. "github.com/smartystreets/goconvey/convey"

"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/stdlib/html"
)

func TestDocumentExists(t *testing.T) {
Convey("DOCUMENT_EXISTS", t, func() {
Convey("Should return error if url is not a string", func() {
_, err := html.DocumentExists(context.Background(), values.None)

So(err, ShouldNotBeNil)
})

Convey("Should return error if options is not an object", func() {
_, err := html.DocumentExists(context.Background(), values.NewString("http://fsdfsdfdsdsf.fdf"), values.None)

So(err, ShouldNotBeNil)
})

Convey("Should return error if headers is not an object", func() {
opts := values.NewObjectWith(values.NewObjectProperty("headers", values.None))
_, err := html.DocumentExists(context.Background(), values.NewString("http://fsdfsdfdsdsf.fdf"), opts)

So(err, ShouldNotBeNil)
})

Convey("Should return 'false' when a website does not exist by a given url", func() {
out, err := html.DocumentExists(context.Background(), values.NewString("http://fsdfsdfdsdsf.fdf"))

So(err, ShouldBeNil)
So(out, ShouldEqual, values.False)
})

Convey("Should return 'true' when a website exists by a given url", func() {
out, err := html.DocumentExists(context.Background(), values.NewString("https://www.google.com/"))

So(err, ShouldBeNil)
So(out, ShouldEqual, values.True)
})
})
}
1 change: 1 addition & 0 deletions pkg/stdlib/html/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func RegisterLib(ns core.Namespace) error {
"CLICK": Click,
"CLICK_ALL": ClickAll,
"DOCUMENT": Open,
"DOCUMENT_EXISTS": DocumentExists,
"DOWNLOAD": Download,
"ELEMENT": Element,
"ELEMENT_EXISTS": ElementExists,
Expand Down