~samwhited/xmpp

xmpp/xtime/time_test.go -rw-r--r-- 1.9 KiB
c9743d9fSam Whited docs: update changelog to include deprecations 9 days 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// 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/ns"
	"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{}
	_ xml.MarshalerAttr   = xtime.Time{}
	_ xml.UnmarshalerAttr = (*xtime.Time)(nil)
)

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(ns.Client, 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)
	}
}

func TestAttrMarshal(t *testing.T) {
	zeroTime := time.Time{}.Add(24 * time.Hour)
	xt := xtime.Time{Time: zeroTime}
	name := xml.Name{Space: "example.net", Local: "foo"}
	attr, err := xt.MarshalXMLAttr(name)
	if err != nil {
		t.Fatalf("unexpected error marshaling attr: %v", err)
	}
	if attr.Name != name {
		t.Fatalf("wrong name for attr: want=%v, got=%v", name, attr.Name)
	}
	const expected = "0001-01-02T00:00:00Z"
	if attr.Value != expected {
		t.Fatalf("wrong value for attr: want=%v, got=%v", expected, attr.Value)
	}

	newTime := &xtime.Time{}
	err = newTime.UnmarshalXMLAttr(attr)
	if err != nil {
		t.Fatalf("unexpected error unmarshaling attr: %v", err)
	}
	if *newTime != xt {
		t.Fatalf("times don't match: want=%v, got=%v", xt, newTime)
	}
}