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

Add support for Uri-Query #48

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,26 @@ func (m *Message) SetPath(s []string) {
m.SetOption(URIPath, s)
}

// Query gets the Query set on this message if any.
func (m Message) Query() []string {
return m.optionStrings(URIQuery)
}

// QueryString gets a path as an ampersand separated string.
func (m Message) QueryString() string {
return strings.Join(m.Query(), "&")
}

// SetQueryString sets a query by an ampersand separated string.
func (m *Message) SetQueryString(s string) {
m.SetQuery(strings.Split(s, "&"))
}

// SetQuery updates or adds a URIQuery attribute on this message.
func (m *Message) SetQuery(s []string) {
m.SetOption(URIQuery, s)
}

// RemoveOption removes all references to an option
func (m *Message) RemoveOption(opID OptionID) {
m.opts = m.opts.Minus(opID)
Expand Down
28 changes: 28 additions & 0 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,34 @@ func TestEncodeSeveral(t *testing.T) {
}
}

func TestEncodeManyQueries(t *testing.T) {
tests := map[string][]string{
"a": []string{"a"},
"axe": []string{"axe"},
"a&b&c&d&e&f&h&g&i&j": []string{"a", "b", "c", "d", "e",
"f", "h", "g", "i", "j"},
}
for p, a := range tests {
m := &Message{Type: Confirmable, Code: GET, MessageID: 12345}
m.SetQueryString(p)
b, err := m.MarshalBinary()
if err != nil {
t.Errorf("Error encoding %#v", p)
t.Fail()
continue
}
m2, err := ParseMessage(b)
if err != nil {
t.Fatalf("Can't parse my own message at %#v: %v", p, err)
}

if !reflect.DeepEqual(m2.Query(), a) {
t.Errorf("Expected %#v, got %#v", a, m2.Path())
t.Fail()
}
}
}

func TestPathAsOption(t *testing.T) {
m := &Message{Type: Confirmable, Code: GET, MessageID: 12345}
m.SetOption(LocationPath, []string{"a", "b"})
Expand Down