-
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
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
package tests
import (
"bytes"
"fmt"
"io"
"os"
"reflect"
"testing"
a "github.com/mariusor/activitypub.go/activitypub"
j "github.com/mariusor/activitypub.go/jsonld"
)
const dir = "./mocks"
var stopOnFailure = false
type testPair struct {
path string
expected bool
blank a.ObjectOrLink
result a.ObjectOrLink
}
type tests map[string]testPair
var allTests = tests{
"empty": testPair{
path: "./mocks/empty.json",
expected: true,
blank: &a.Object{},
result: &a.Object{},
},
"link_simple": testPair{
path: "./mocks/link_simple.json",
expected: true,
blank: &a.Link{},
result: &a.Link{
Type: a.LinkType,
Href: a.URI("http://example.org/abc"),
HrefLang: a.LangRef("en"),
MediaType: a.MimeType("text/html"),
Name: a.NaturalLanguageValue{
a.LangRef("-"): "An example link",
},
},
},
"object_with_url": testPair{
path: "./mocks/object_with_url.json",
expected: true,
blank: &a.Object{},
result: &a.Object{
URL: a.URI("http://littr.git/api/accounts/system"),
},
},
"object_simple": testPair{
path: "./mocks/object_simple.json",
expected: true,
blank: &a.Object{},
result: &a.Object{
Type: a.ObjectType,
ID: a.ObjectID("http://www.test.example/object/1"),
Name: a.NaturalLanguageValue{
a.LangRef("-"): "A Simple, non-specific object",
},
},
},
//"activity_simple": testPair{
// path: "./mocks/activity_simple.json",
// expected: false,
// blank: &a.Activity{},
// result: &a.Activity{
// Type: a.ActivityType,
// Summary: a.NaturalLanguageValue{a.LangRef("-"): "Sally did something to a note"},
// Actor: a.Actor(a.Person{
// Type: a.PersonType,
// Name: a.NaturalLanguageValue{
// a.LangRef("-"): "Sally",
// },
// }),
// payloadWithContext: a.payloadWithContext{
// Type: a.NoteType,
// Name: a.NaturalLanguageValue{
// a.LangRef("-"): "A Note",
// },
// },
// },
//},
"person_with_outbox": testPair{
path: "./mocks/person_with_outbox.json",
expected: true,
blank: &a.Person{},
result: &a.Person{
ID: a.ObjectID("http://example.com/accounts/ana"),
Type: a.PersonType,
Name: a.NaturalLanguageValue{
a.LangRef("-"): "ana",
},
PreferredUsername: a.NaturalLanguageValue{
a.LangRef("-"): "Ana",
},
URL: a.URI("http://example.com/accounts/ana"),
Outbox: &a.OutboxStream{
ID: a.ObjectID("http://example.com/accounts/ana/outbox"),
Type: a.OrderedCollectionType,
URL: a.URI("http://example.com/outbox"),
},
},
},
}
func getFileContents(path string) ([]byte, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
data := make([]byte, 512)
io.ReadFull(f, data)
data = bytes.Trim(data, "\x00")
return data, nil
}
func Test_ActivityPubUnmarshall(t *testing.T) {
var err error
var f = t.Errorf
if stopOnFailure {
f = t.Fatalf
}
if len(allTests) == 0 {
t.Skip("No tests found")
}
for k, pair := range allTests {
var data []byte
data, err = getFileContents(pair.path)
if err != nil {
f("Error: %s for %s", err, pair.path)
continue
}
object := pair.blank
err = j.Unmarshal(data, object)
if err != nil {
f("Error: %s for %s", err, data)
continue
}
expLbl := ""
if !pair.expected {
expLbl = "not be "
}
if pair.expected != reflect.DeepEqual(object, pair.result) {
f("\n%#v\n should %sequal to\n%#v", object, expLbl, pair.result)
oj, e1 := j.Marshal(object)
if e1 != nil {
f(e1.Error())
}
tj, e2 := j.Marshal(pair.result)
if e2 != nil {
f(e2.Error())
}
f("\n%s\n should %sequal to\n%s", oj, expLbl, tj)
continue
}
//fmt.Printf("%#v", object)
if err == nil {
fmt.Printf(" --- %s: %s\n %s\n", "PASS", k, pair.path)
}
}
}