// WriteElement converts the element to bytes and writes to the underlying // tcp connection. This method should generally be used for basic elements such // as those used during SASL negotiation. WriteStanzas should be used when // sending stanzas. func (t *TCP) WriteElement(el element.Element) error { var b []byte b = el.WriteBytes() _, err := t.Write(b) return err }
func TestNext(t *testing.T) { t.Parallel() var want, got interface{} var err error var el element.Element pipe1, pipe2 := net.Pipe() el = element.New("testing").AddAttr("foo", "bar"). SetText("random text"). AddChild(element.New("baz-quux")) tcpTsp := NewTCP(pipe1, stream.Receiving, nil, true) // Should be able to get a token from the transport go func() { _, err := pipe2.Write(el.WriteBytes()) if err != nil { t.Errorf("An unexpected error occurred: %s", err) } }() want = el got, err = tcpTsp.Next() if err != nil { t.Errorf("An unexpected error occurred: %s", err) } if !reflect.DeepEqual(want, got) { t.Error("Should be able to get a token from the transport.") t.Errorf("\nWant:%+v\nGot :%+v", want, got) } // Stream element should return token and not attempt to read the entire stream. pipe1, pipe2 = net.Pipe() tcpTsp = NewTCP(pipe1, stream.Receiving, nil, true) go func() { _, err := pipe2.Write(stream.Header{}.WriteBytes()) if err != nil { t.Errorf("An unexpected error occurred: %s", err) } _, err = pipe2.Write([]byte("<foo/></stream:stream>")) if err != nil { t.Errorf("An unexpected error occurred: %s", err) } }() el, err = tcpTsp.Next() if err != nil { t.Errorf("An unexpected error occurred: %s", err) } if el.Space != namespace.Stream || el.Tag != "stream" { t.Error("Stream element should return token and not attempt to read the entire stream.") } got, err = tcpTsp.Next() if err != nil { t.Errorf("An unexpected error occurred: %s", err) } want = element.New("foo") if !reflect.DeepEqual(want, got) { t.Error("Stream element should return token and not attempt to read the entire stream.") t.Errorf("\nWant:%+v\nGot :%+v", want, got) } }