Skip to content

Commit

Permalink
added parse query subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianrudnik committed Apr 3, 2019
1 parent a381d02 commit 28f39fd
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
20 changes: 18 additions & 2 deletions cmd/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/spf13/cobra"
"html/template"
"net/url"
"strings"
)

var parseCmd = &cobra.Command{
Expand Down Expand Up @@ -92,13 +93,28 @@ var parseQueryCmd = &cobra.Command{
Short: "parses the given query string and returns them as json or formatted template",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
_, err := url.ParseQuery(args[0])
// remove leading question mark
in := strings.TrimLeft(args[0], "?")

parsed, err := url.ParseQuery(in)

if err != nil {
return err
}

done, err := doFormat(cmd, parsed)

if err != nil {
return err
}

// @todo output(cmd, out)
if done {
return nil
}

if err := doJson(cmd, parsed); err != nil {
return err
}

return nil
},
Expand Down
55 changes: 54 additions & 1 deletion cmd/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"encoding/json"
"github.com/adrianrudnik/uritool/cmd/cmdtest"
"net/url"
"strings"
"testing"
)
Expand Down Expand Up @@ -254,7 +255,7 @@ func TestParseValidUriWithFormat(t *testing.T) {
}
}

func TestParseStringWithFormat(t *testing.T) {
func TestParseValidUriStringWithFormat(t *testing.T) {
setup()

in := "http://www.example.com/"
Expand All @@ -265,3 +266,55 @@ func TestParseStringWithFormat(t *testing.T) {
t.Errorf("Expected format is wrong: %s != %s", out, expected)
}
}

func TestParseValidQuery(t *testing.T) {
setup()

in := "this=is&this=isnot"
out, _ := cmdtest.ExecuteCommand(rootCmd, "-n", "parse", "query", in)

var result url.Values
err := json.Unmarshal([]byte(out), &result)

if err != nil {
t.Errorf("Could not validate JSON: %s", err)
}

if len(result.Get("this")) != 2 {
t.Errorf("Expected query parameter count is wrong: %d != %d", len(result.Get("this")), 1)
}

if result["this"][0] != "is" {
t.Errorf("Expected query parameter array element 0 is wrong: %s != %s", result["this"][0], "is")
}

if result["this"][1] != "isnot" {
t.Errorf("Expected query parameter array element 0 is wrong: %s != %s", result["this"][1], "isnot")
}
}

func TestParseValidQueryWithLeadingQuestionMark(t *testing.T) {
setup()

in := "?this=is&this=isnot"
out, _ := cmdtest.ExecuteCommand(rootCmd, "-n", "parse", "query", in)

var result url.Values
err := json.Unmarshal([]byte(out), &result)

if err != nil {
t.Errorf("Could not validate JSON: %s", err)
}

if len(result.Get("this")) != 2 {
t.Errorf("Expected query parameter count is wrong: %d != %d", len(result.Get("this")), 1)
}

if result["this"][0] != "is" {
t.Errorf("Expected query parameter array element 0 is wrong: %s != %s", result["this"][0], "is")
}

if result["this"][1] != "isnot" {
t.Errorf("Expected query parameter array element 0 is wrong: %s != %s", result["this"][1], "isnot")
}
}

0 comments on commit 28f39fd

Please sign in to comment.