Skip to content

Commit

Permalink
Resolves #32 - Add support for URL Encoded Filters
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-r-west committed Feb 17, 2024
1 parent 19c1d12 commit 11a1b5e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
18 changes: 16 additions & 2 deletions external/epsearchast/v3/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package epsearchast_v3
import (
"encoding/json"
"fmt"
"net/url"
"strings"
)

Expand All @@ -21,8 +22,21 @@ func GetAst(jsonTxt string) (*AstNode, error) {
err := json.Unmarshal([]byte(jsonTxt), astNode)

if err != nil {
return nil, fmt.Errorf("could not parse filter:%w", err)
} else if err := astNode.checkValid(); err != nil {
// url decode jsonTxt
decoded, urlDecodingError := url.QueryUnescape(jsonTxt)

if urlDecodingError == nil {
urlDecodingError = json.Unmarshal([]byte(decoded), astNode)

if urlDecodingError != nil {
return nil, fmt.Errorf("could not parse filter:%w, error parsing decoded filter: %v", err, urlDecodingError)
}
} else {
return nil, fmt.Errorf("could not parse filter:%w, error decoding: %v", err, urlDecodingError)
}
}

if err := astNode.checkValid(); err != nil {
return nil, fmt.Errorf("error validating filter:%w", err)
} else {
return astNode, nil
Expand Down
45 changes: 45 additions & 0 deletions external/epsearchast/v3/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package epsearchast_v3

import (
"github.com/stretchr/testify/require"
"net/url"
"testing"
)

Expand Down Expand Up @@ -283,3 +284,47 @@ func TestAndReturnsErrorWithAnInvalidChild(t *testing.T) {
require.ErrorContains(t, err, "unknown operator FOO")
require.Nil(t, astNode)
}

func TestValidObjectThatIsUrlEncodedReturnsAst(t *testing.T) {
// Fixture Setup
// language=JSON
jsonTxt := `
{
"type": "EQ",
"args": [ "status", "paid"]
}
`
// Execute SUT
astNode, err := GetAst(url.QueryEscape(jsonTxt))

// Verify
require.NoError(t, err)
require.NotNil(t, astNode)
}

func TestInValidObjectThatIsUrlEncodedReturnsError(t *testing.T) {
// Fixture Setup
jsonTxt := `
{
"type": "EQ",
"args": [ "status", "paid"]
`
// Execute SUT
astNode, err := GetAst(url.QueryEscape(jsonTxt))

// Verify
require.Error(t, err)
require.ErrorContains(t, err, "unexpected end of JSON input")
require.Nil(t, astNode)
}

func TestInValidUrlEncodingReturnsError(t *testing.T) {
// Fixture Setup
// Execute SUT
astNode, err := GetAst("%4")

// Verify
require.Error(t, err)
require.ErrorContains(t, err, "invalid URL escape")
require.Nil(t, astNode)
}

0 comments on commit 11a1b5e

Please sign in to comment.