// Copyright 2016 The Mellium Contributors. // Use of this source code is governed by the BSD 2-clause // license that can be found in the LICENSE file. package stream_test import ( "bytes" "fmt" "io" "strings" "testing" "mellium.im/xmpp/internal/decl" "mellium.im/xmpp/internal/stream" ) func TestSendNewS2S(t *testing.T) { for i, tc := range []struct { s2s bool id bool output string err error }{ {true, true, ` id='abc' `, nil}, {false, true, ` id='abc' `, nil}, {true, true, ` xmlns='jabber:server' `, nil}, {false, false, ` xmlns='jabber:client' `, nil}, } { name := fmt.Sprintf("%d", i) t.Run(name, func(t *testing.T) { var b bytes.Buffer ids := "" if tc.id { ids = "abc" } _, err := stream.Send(&b, tc.s2s, stream.Version{Major: 1, Minor: 0}, "und", "example.net", "test@example.net", ids) str := b.String() if !strings.HasPrefix(str, decl.XMLHeader) { t.Errorf("Expected string to start with XML header but got: %s", str) } str = strings.TrimPrefix(str, decl.XMLHeader) switch { case err != tc.err: t.Errorf("Error did not match, excepted `%s` but got `%s`.", tc.err, err) case !strings.Contains(str, tc.output): t.Errorf("Expected string to contain `%s` but got: %s", tc.output, str) case !strings.HasPrefix(str, ``): t.Errorf("Expected string to end with xmlns:stream=… but got: %s", str) } }) } } type errWriter struct{} func (errWriter) Write(p []byte) (int, error) { return 0, io.ErrUnexpectedEOF } type nopReader struct{} func (nopReader) Read(p []byte) (n int, err error) { return 0, nil } func TestSendNewS2SReturnsWriteErr(t *testing.T) { _, err := stream.Send(struct { io.Reader io.Writer }{ nopReader{}, errWriter{}, }, true, stream.Version{Major: 1, Minor: 0}, "und", "example.net", "test@example.net", "abc") if err != io.ErrUnexpectedEOF { t.Errorf("Expected errWriterErr (%s) but got `%s`", io.ErrUnexpectedEOF, err) } }