attributes: implement Setter for RawAttribute

This commit is contained in:
Aleksandr Razumov
2018-11-16 04:45:20 +03:00
parent 902ea3904b
commit db2ead48d4
3 changed files with 27 additions and 0 deletions

1
api/stun1.18.0.txt Normal file
View File

@@ -0,0 +1 @@
pkg github.com/gortc/stun, method (RawAttribute) AddTo(*Message) error

View File

@@ -146,6 +146,13 @@ type RawAttribute struct {
Value []byte
}
// AddTo implements Setter, adding attribute as a.Type with a.Value and ignoring
// the Length field.
func (a RawAttribute) AddTo(m *Message) error {
m.Add(a.Type, a.Value)
return nil
}
// Equal returns true if a == b.
func (a RawAttribute) Equal(b RawAttribute) bool {
if a.Type != b.Type {

View File

@@ -1,6 +1,7 @@
package stun
import (
"bytes"
"testing"
)
@@ -21,6 +22,24 @@ func BenchmarkMessage_Get(b *testing.B) {
}
}
func TestRawAttribute_AddTo(t *testing.T) {
v := []byte{1, 2, 3, 4}
m, err := Build(RawAttribute{
Type: AttrData,
Value: v,
})
if err != nil {
t.Fatal(err)
}
gotV, gotErr := m.Get(AttrData)
if gotErr != nil {
t.Fatal(gotErr)
}
if !bytes.Equal(gotV, v) {
t.Error("value mismatch")
}
}
func TestMessage_GetNoAllocs(t *testing.T) {
m := New()
NewSoftware("c").AddTo(m)