Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: add support for passing contexts
  • Loading branch information
nickylogan committed Nov 19, 2020
1 parent a558c0a commit 538ef10
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions message.go
Expand Up @@ -128,6 +128,12 @@ func buildParseQuery(req *MessageRequest) string {
if req.Tag != "" {
q += fmt.Sprintf("&tag=%s", req.Tag)
}
if req.Context != nil {
b, _ := json.Marshal(req.Context)
if b != nil {
q += fmt.Sprintf("&context=%s", url.PathEscape(string(b)))
}
}

return q
}
32 changes: 32 additions & 0 deletions message_test.go
Expand Up @@ -6,6 +6,7 @@ import (
"bytes"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
)
Expand Down Expand Up @@ -146,3 +147,34 @@ func TestSpeech(t *testing.T) {
t.Fatalf("expected \n\tmsg %v \n\tgot %v", wantMessage, msg)
}
}

func Test_buildParseQuery(t *testing.T) {
want := "?q=" + url.PathEscape("hello world") +
"&n=1&tag=tag" +
"&context=" +
url.PathEscape("{"+
"\"reference_time\":\"2014-10-30T12:18:45-07:00\","+
"\"timezone\":\"America/Los_Angeles\","+
"\"locale\":\"en_US\","+
"\"coords\":{\"lat\":32.47104,\"long\":-122.14703}"+
"}")

got := buildParseQuery(&MessageRequest{
Query: "hello world",
N: 1,
Tag: "tag",
Context: &MessageContext{
Locale: "en_US",
Coords: MessageCoords{
Lat: 32.47104,
Long: -122.14703,
},
Timezone: "America/Los_Angeles",
ReferenceTime: "2014-10-30T12:18:45-07:00",
},
})

if got != want {
t.Fatalf("expected \n\tquery = %v \n\tgot = %v", want, got)
}
}

0 comments on commit 538ef10

Please sign in to comment.