~samwhited/xmpp

ref: 467e8c4a0b977d765eadf64f02ebb5edee0ce805 xmpp/xtime/time_test.go -rw-r--r-- 1.1 KiB
467e8c4aSam Whited component: add integration tests 1 year, 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2020 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 xtime_test

import (
	"context"
	"encoding/xml"
	"testing"
	"time"

	"mellium.im/xmlstream"
	"mellium.im/xmpp/internal/xmpptest"
	"mellium.im/xmpp/mux"
	"mellium.im/xmpp/xtime"
)

var (
	_ xml.Marshaler       = xtime.Time{}
	_ xml.Unmarshaler     = (*xtime.Time)(nil)
	_ xmlstream.Marshaler = xtime.Time{}
	_ xmlstream.WriterTo  = xtime.Time{}
)

type tokenReadEncoder struct {
	xml.TokenReader
	xmlstream.Encoder
}

func TestRoundTrip(t *testing.T) {
	serverTime := time.Time{}
	serverTime = serverTime.Add(24 * time.Hour * 7 * 52)
	h := xtime.Handler{
		TimeFunc: func() time.Time {
			return serverTime
		},
	}
	m := mux.New(xtime.Handle(h))
	cs := xmpptest.NewClientServer(
		xmpptest.ServerHandler(m),
	)

	respTime, err := xtime.Get(context.Background(), cs.Client, cs.Server.LocalAddr())
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	if !serverTime.Equal(respTime) {
		t.Errorf("wrong time: want=%v, got=%v", serverTime, respTime)
	}
}