-
Notifications
You must be signed in to change notification settings - Fork 23
/
getobject_test.go
219 lines (185 loc) · 6.03 KB
/
getobject_test.go
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
provides the photo extraction core testing
*/
package rets
import (
"io/ioutil"
"net/http"
"net/textproto"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetObject(t *testing.T) {
header := http.Header{}
textproto.MIMEHeader(header).Add("Content-Type", "image/jpeg")
textproto.MIMEHeader(header).Add("Content-ID", "123456")
textproto.MIMEHeader(header).Add("Object-ID", "1")
textproto.MIMEHeader(header).Add("Preferred", "1")
textproto.MIMEHeader(header).Add("UID", "1a234234234")
textproto.MIMEHeader(header).Add("Content-Description", "Outhouse")
textproto.MIMEHeader(header).Add("Content-Sub-Description", "The urinal")
textproto.MIMEHeader(header).Add("Location", "http://www.simpleboundary.com/image-5.jpg")
var body = `<binary data 1>`
response := GetObjectResponse{
Response: &http.Response{
Header: header,
Body: ioutil.NopCloser(strings.NewReader(body)),
},
}
defer response.Close()
var objects []*Object
err := response.ForEach(func(o *Object, err error) error {
objects = append(objects, o)
return nil
})
assert.Nil(t, err)
assert.Equal(t, 1, len(objects))
o := objects[0]
assert.Equal(t, true, o.Preferred)
assert.Equal(t, "image/jpeg", o.ContentType)
assert.Equal(t, "123456", o.ContentID)
assert.Equal(t, 1, o.ObjectID)
assert.Equal(t, "1a234234234", o.UID)
assert.Equal(t, "Outhouse", o.Description)
assert.Equal(t, "The urinal", o.SubDescription)
assert.Equal(t, "<binary data 1>", string(o.Blob))
assert.Equal(t, "http://www.simpleboundary.com/image-5.jpg", o.Location)
assert.Equal(t, false, o.RetsError)
}
func TestSingleObjectNoObjectID(t *testing.T) {
header := http.Header{}
textproto.MIMEHeader(header).Add("Content-Type", "image/jpeg")
textproto.MIMEHeader(header).Add("Preferred", "1")
textproto.MIMEHeader(header).Add("Content-Description", "Outhouse")
textproto.MIMEHeader(header).Add("Content-Sub-Description", "The urinal")
textproto.MIMEHeader(header).Add("Location", "http://www.simpleboundary.com/image-5.jpg")
var body = `<binary data 1>`
response := GetObjectResponse{
Response: &http.Response{
Header: header,
Body: ioutil.NopCloser(strings.NewReader(body)),
},
}
defer response.Close()
var objects []*Object
err := response.ForEach(func(o *Object, err error) error {
objects = append(objects, o)
return nil
})
assert.Nil(t, err)
assert.Equal(t, 1, len(objects))
o := objects[0]
assert.Equal(t, true, o.Preferred)
assert.Equal(t, -1, o.ObjectID)
assert.Equal(t, "image/jpeg", o.ContentType)
assert.Equal(t, "Outhouse", o.Description)
assert.Equal(t, "The urinal", o.SubDescription)
assert.Equal(t, "<binary data 1>", string(o.Blob))
assert.Equal(t, "http://www.simpleboundary.com/image-5.jpg", o.Location)
assert.Equal(t, false, o.RetsError)
}
var boundary = "simple boundary"
var contentType = `multipart/parallel; boundary="simple boundary"`
var multipartBody = `--simple boundary
Content-Type: image/jpeg
Content-ID: 123456
Object-ID: 1
Preferred: 1
ObjectData: ListingKey=123456
ObjectData: ListDate=2013-05-01T12:34:34.8-0500
<binary data 1>
--simple boundary
Content-Type: image/jpeg
Content-ID: 123456
Object-ID: 2
UID: 1a234234234
<binary data 2>
--simple boundary
Content-Type: image/jpeg
Content-ID: 123456
Object-ID: 3
Content-Description: Outhouse
Content-Sub-Description: The urinal
<binary data 3>
--simple boundary
Content-Type: text/xml
Content-ID: 123457
Object-ID: 4
RETS-Error: 1
<RETS ReplyCode="20403" ReplyText="There is no object with that Object-ID"/>
--simple boundary
Content-Type: image/jpeg
Content-ID: 123457
Object-ID: 5
Location: http://www.simpleboundary.com/image-5.jpg
--simple boundary
Content-Type: image/jpeg
Content-ID: 123457
Object-ID: 6
Location: http://www.simpleboundary.com/image-6.jpg
<binary data 6>
--simple boundary
Content-Type: image/jpeg
Content-ID: 123457
Object-ID: 7
Location: http://www.simpleboundary.com/image-7.jpg
<RETS ReplyCode="0" ReplyText="Found it!"/>
--simple boundary--`
func TestGetObjects(t *testing.T) {
headers := http.Header{}
headers.Add("Content-Type", contentType)
response := GetObjectResponse{
Response: &http.Response{
Header: headers,
Body: ioutil.NopCloser(strings.NewReader(multipartBody)),
},
}
defer response.Close()
var objects []*Object
response.ForEach(func(o *Object, err error) error {
assert.Nil(t, err)
objects = append(objects, o)
return nil
})
o1 := objects[0]
assert.Equal(t, true, o1.Preferred)
assert.Equal(t, "image/jpeg", o1.ContentType)
assert.Equal(t, "123456", o1.ContentID)
assert.Equal(t, 1, o1.ObjectID)
assert.Equal(t, "<binary data 1>", string(o1.Blob))
assert.Equal(t, "123456", o1.ObjectData["ListingKey"])
assert.Equal(t, "2013-05-01T12:34:34.8-0500", o1.ObjectData["ListDate"])
o2 := objects[1]
assert.Equal(t, 2, o2.ObjectID)
assert.Equal(t, "1a234234234", o2.UID)
o3 := objects[2]
assert.Equal(t, 3, o3.ObjectID)
assert.Equal(t, "Outhouse", o3.Description)
assert.Equal(t, "The urinal", o3.SubDescription)
o4 := objects[3]
assert.Equal(t, true, o4.RetsError)
assert.Equal(t, "text/xml", o4.ContentType)
assert.Equal(t, "There is no object with that Object-ID", o4.RetsMessage.Text)
assert.Equal(t, StatusObjectNotFound, o4.RetsMessage.Code)
o5 := objects[4]
assert.Equal(t, "http://www.simpleboundary.com/image-5.jpg", o5.Location)
assert.Equal(t, "image/jpeg", o5.ContentType)
assert.Equal(t, "123457", o5.ContentID)
assert.Equal(t, 5, o5.ObjectID)
assert.Equal(t, "", string(o5.Blob))
o6 := objects[5]
assert.Equal(t, "http://www.simpleboundary.com/image-6.jpg", o6.Location)
assert.Equal(t, "image/jpeg", o6.ContentType)
assert.Equal(t, "123457", o6.ContentID)
assert.Equal(t, 6, o6.ObjectID)
assert.Equal(t, "<binary data 6>", string(o6.Blob))
assert.Nil(t, o6.RetsMessage, "should not be the zerod object")
o7 := objects[6]
assert.Equal(t, "http://www.simpleboundary.com/image-7.jpg", o7.Location)
assert.Equal(t, "image/jpeg", o7.ContentType)
assert.Equal(t, "123457", o7.ContentID)
assert.Equal(t, 7, o7.ObjectID)
assert.Equal(t, "", string(o7.Blob))
assert.Equal(t, "Found it!", o7.RetsMessage.Text)
}