1 |
747 |
jeremybenn |
// Copyright 2009 The Go Authors. All rights reserved.
|
2 |
|
|
// Use of this source code is governed by a BSD-style
|
3 |
|
|
// license that can be found in the LICENSE file.
|
4 |
|
|
|
5 |
|
|
package xml
|
6 |
|
|
|
7 |
|
|
import (
|
8 |
|
|
"reflect"
|
9 |
|
|
"testing"
|
10 |
|
|
)
|
11 |
|
|
|
12 |
|
|
// Stripped down Atom feed data structures.
|
13 |
|
|
|
14 |
|
|
func TestUnmarshalFeed(t *testing.T) {
|
15 |
|
|
var f Feed
|
16 |
|
|
if err := Unmarshal([]byte(atomFeedString), &f); err != nil {
|
17 |
|
|
t.Fatalf("Unmarshal: %s", err)
|
18 |
|
|
}
|
19 |
|
|
if !reflect.DeepEqual(f, atomFeed) {
|
20 |
|
|
t.Fatalf("have %#v\nwant %#v", f, atomFeed)
|
21 |
|
|
}
|
22 |
|
|
}
|
23 |
|
|
|
24 |
|
|
// hget http://codereview.appspot.com/rss/mine/rsc
|
25 |
|
|
const atomFeedString = `
|
26 |
|
|
|
27 |
|
|
Code Review - My issueshttp://codereview.appspot.com/2009-10-04T01:35:58+00:00rietveld<>rietveld: an attempt at pubsubhubbub
|
28 |
|
|
2009-10-04T01:35:58+00:00email-address-removedurn:md5:134d9179c41f806be79b3a5f7877d19a
|
29 |
|
|
An attempt at adding pubsubhubbub support to Rietveld.
|
30 |
|
|
http://code.google.com/p/pubsubhubbub
|
31 |
|
|
http://code.google.com/p/rietveld/issues/detail?id=155
|
32 |
|
|
|
33 |
|
|
The server side of the protocol is trivial:
|
34 |
|
|
1. add a <link rel="hub" href="hub-server"> tag to all
|
35 |
|
|
feeds that will be pubsubhubbubbed.
|
36 |
|
|
2. every time one of those feeds changes, tell the hub
|
37 |
|
|
with a simple POST request.
|
38 |
|
|
|
39 |
|
|
I have tested this by adding debug prints to a local hub
|
40 |
|
|
server and checking that the server got the right publish
|
41 |
|
|
requests.
|
42 |
|
|
|
43 |
|
|
I can't quite get the server to work, but I think the bug
|
44 |
|
|
is not in my code. I think that the server expects to be
|
45 |
|
|
able to grab the feed and see the feed's actual URL in
|
46 |
|
|
the link rel="self", but the default value for that drops
|
47 |
|
|
the :port from the URL, and I cannot for the life of me
|
48 |
|
|
figure out how to get the Atom generator deep inside
|
49 |
|
|
django not to do that, or even where it is doing that,
|
50 |
|
|
or even what code is running to generate the Atom feed.
|
51 |
|
|
(I thought I knew but I added some assert False statements
|
52 |
|
|
and it kept running!)
|
53 |
|
|
|
54 |
|
|
Ignoring that particular problem, I would appreciate
|
55 |
|
|
feedback on the right way to get the two values at
|
56 |
|
|
the top of feeds.py marked NOTE(rsc).
|
57 |
|
|
|
58 |
|
|
|
59 |
|
|
rietveld: correct tab handling
|
60 |
|
|
2009-10-03T23:02:17+00:00email-address-removedurn:md5:0a2a4f19bb815101f0ba2904aed7c35a
|
61 |
|
|
This fixes the buggy tab rendering that can be seen at
|
62 |
|
|
http://codereview.appspot.com/116075/diff/1/2
|
63 |
|
|
|
64 |
|
|
The fundamental problem was that the tab code was
|
65 |
|
|
not being told what column the text began in, so it
|
66 |
|
|
didn't know where to put the tab stops. Another problem
|
67 |
|
|
was that some of the code assumed that string byte
|
68 |
|
|
offsets were the same as column offsets, which is only
|
69 |
|
|
true if there are no tabs.
|
70 |
|
|
|
71 |
|
|
In the process of fixing this, I cleaned up the arguments
|
72 |
|
|
to Fold and ExpandTabs and renamed them Break and
|
73 |
|
|
_ExpandTabs so that I could be sure that I found all the
|
74 |
|
|
call sites. I also wanted to verify that ExpandTabs was
|
75 |
|
|
not being used from outside intra_region_diff.py.
|
76 |
|
|
|
77 |
|
|
|
78 |
|
|
`
|
79 |
|
|
|
80 |
|
|
type Feed struct {
|
81 |
|
|
XMLName Name `xml:"http://www.w3.org/2005/Atom feed"`
|
82 |
|
|
Title string `xml:"title"`
|
83 |
|
|
Id string `xml:"id"`
|
84 |
|
|
Link []Link `xml:"link"`
|
85 |
|
|
Updated Time `xml:"updated"`
|
86 |
|
|
Author Person `xml:"author"`
|
87 |
|
|
Entry []Entry `xml:"entry"`
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
type Entry struct {
|
91 |
|
|
Title string `xml:"title"`
|
92 |
|
|
Id string `xml:"id"`
|
93 |
|
|
Link []Link `xml:"link"`
|
94 |
|
|
Updated Time `xml:"updated"`
|
95 |
|
|
Author Person `xml:"author"`
|
96 |
|
|
Summary Text `xml:"summary"`
|
97 |
|
|
}
|
98 |
|
|
|
99 |
|
|
type Link struct {
|
100 |
|
|
Rel string `xml:"rel,attr"`
|
101 |
|
|
Href string `xml:"href,attr"`
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
type Person struct {
|
105 |
|
|
Name string `xml:"name"`
|
106 |
|
|
URI string `xml:"uri"`
|
107 |
|
|
Email string `xml:"email"`
|
108 |
|
|
InnerXML string `xml:",innerxml"`
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
type Text struct {
|
112 |
|
|
Type string `xml:"type,attr"`
|
113 |
|
|
Body string `xml:",chardata"`
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
type Time string
|
117 |
|
|
|
118 |
|
|
var atomFeed = Feed{
|
119 |
|
|
XMLName: Name{"http://www.w3.org/2005/Atom", "feed"},
|
120 |
|
|
Title: "Code Review - My issues",
|
121 |
|
|
Link: []Link{
|
122 |
|
|
{Rel: "alternate", Href: "http://codereview.appspot.com/"},
|
123 |
|
|
{Rel: "self", Href: "http://codereview.appspot.com/rss/mine/rsc"},
|
124 |
|
|
},
|
125 |
|
|
Id: "http://codereview.appspot.com/",
|
126 |
|
|
Updated: "2009-10-04T01:35:58+00:00",
|
127 |
|
|
Author: Person{
|
128 |
|
|
Name: "rietveld<>",
|
129 |
|
|
InnerXML: "rietveld<>",
|
130 |
|
|
},
|
131 |
|
|
Entry: []Entry{
|
132 |
|
|
{
|
133 |
|
|
Title: "rietveld: an attempt at pubsubhubbub\n",
|
134 |
|
|
Link: []Link{
|
135 |
|
|
{Rel: "alternate", Href: "http://codereview.appspot.com/126085"},
|
136 |
|
|
},
|
137 |
|
|
Updated: "2009-10-04T01:35:58+00:00",
|
138 |
|
|
Author: Person{
|
139 |
|
|
Name: "email-address-removed",
|
140 |
|
|
InnerXML: "email-address-removed",
|
141 |
|
|
},
|
142 |
|
|
Id: "urn:md5:134d9179c41f806be79b3a5f7877d19a",
|
143 |
|
|
Summary: Text{
|
144 |
|
|
Type: "html",
|
145 |
|
|
Body: `
|
146 |
|
|
An attempt at adding pubsubhubbub support to Rietveld.
|
147 |
|
|
http://code.google.com/p/pubsubhubbub
|
148 |
|
|
http://code.google.com/p/rietveld/issues/detail?id=155
|
149 |
|
|
|
150 |
|
|
The server side of the protocol is trivial:
|
151 |
|
|
1. add a <link rel="hub" href="hub-server"> tag to all
|
152 |
|
|
feeds that will be pubsubhubbubbed.
|
153 |
|
|
2. every time one of those feeds changes, tell the hub
|
154 |
|
|
with a simple POST request.
|
155 |
|
|
|
156 |
|
|
I have tested this by adding debug prints to a local hub
|
157 |
|
|
server and checking that the server got the right publish
|
158 |
|
|
requests.
|
159 |
|
|
|
160 |
|
|
I can't quite get the server to work, but I think the bug
|
161 |
|
|
is not in my code. I think that the server expects to be
|
162 |
|
|
able to grab the feed and see the feed's actual URL in
|
163 |
|
|
the link rel="self", but the default value for that drops
|
164 |
|
|
the :port from the URL, and I cannot for the life of me
|
165 |
|
|
figure out how to get the Atom generator deep inside
|
166 |
|
|
django not to do that, or even where it is doing that,
|
167 |
|
|
or even what code is running to generate the Atom feed.
|
168 |
|
|
(I thought I knew but I added some assert False statements
|
169 |
|
|
and it kept running!)
|
170 |
|
|
|
171 |
|
|
Ignoring that particular problem, I would appreciate
|
172 |
|
|
feedback on the right way to get the two values at
|
173 |
|
|
the top of feeds.py marked NOTE(rsc).
|
174 |
|
|
|
175 |
|
|
|
176 |
|
|
`,
|
177 |
|
|
},
|
178 |
|
|
},
|
179 |
|
|
{
|
180 |
|
|
Title: "rietveld: correct tab handling\n",
|
181 |
|
|
Link: []Link{
|
182 |
|
|
{Rel: "alternate", Href: "http://codereview.appspot.com/124106"},
|
183 |
|
|
},
|
184 |
|
|
Updated: "2009-10-03T23:02:17+00:00",
|
185 |
|
|
Author: Person{
|
186 |
|
|
Name: "email-address-removed",
|
187 |
|
|
InnerXML: "email-address-removed",
|
188 |
|
|
},
|
189 |
|
|
Id: "urn:md5:0a2a4f19bb815101f0ba2904aed7c35a",
|
190 |
|
|
Summary: Text{
|
191 |
|
|
Type: "html",
|
192 |
|
|
Body: `
|
193 |
|
|
This fixes the buggy tab rendering that can be seen at
|
194 |
|
|
http://codereview.appspot.com/116075/diff/1/2
|
195 |
|
|
|
196 |
|
|
The fundamental problem was that the tab code was
|
197 |
|
|
not being told what column the text began in, so it
|
198 |
|
|
didn't know where to put the tab stops. Another problem
|
199 |
|
|
was that some of the code assumed that string byte
|
200 |
|
|
offsets were the same as column offsets, which is only
|
201 |
|
|
true if there are no tabs.
|
202 |
|
|
|
203 |
|
|
In the process of fixing this, I cleaned up the arguments
|
204 |
|
|
to Fold and ExpandTabs and renamed them Break and
|
205 |
|
|
_ExpandTabs so that I could be sure that I found all the
|
206 |
|
|
call sites. I also wanted to verify that ExpandTabs was
|
207 |
|
|
not being used from outside intra_region_diff.py.
|
208 |
|
|
|
209 |
|
|
|
210 |
|
|
`,
|
211 |
|
|
},
|
212 |
|
|
},
|
213 |
|
|
},
|
214 |
|
|
}
|
215 |
|
|
|
216 |
|
|
const pathTestString = `
|
217 |
|
|
|
218 |
|
|
1
|
219 |
|
|
|
220 |
|
|
|
221 |
|
|
A
|
222 |
|
|
|
223 |
|
|
|
224 |
|
|
B
|
225 |
|
|
|
226 |
|
|
|
227 |
|
|
C
|
228 |
|
|
D
|
229 |
|
|
|
230 |
|
|
<_>
|
231 |
|
|
E
|
232 |
|
|
|
233 |
|
|
|
234 |
|
|
2
|
235 |
|
|
|
236 |
|
|
`
|
237 |
|
|
|
238 |
|
|
type PathTestItem struct {
|
239 |
|
|
Value string
|
240 |
|
|
}
|
241 |
|
|
|
242 |
|
|
type PathTestA struct {
|
243 |
|
|
Items []PathTestItem `xml:">Item1"`
|
244 |
|
|
Before, After string
|
245 |
|
|
}
|
246 |
|
|
|
247 |
|
|
type PathTestB struct {
|
248 |
|
|
Other []PathTestItem `xml:"Items>Item1"`
|
249 |
|
|
Before, After string
|
250 |
|
|
}
|
251 |
|
|
|
252 |
|
|
type PathTestC struct {
|
253 |
|
|
Values1 []string `xml:"Items>Item1>Value"`
|
254 |
|
|
Values2 []string `xml:"Items>Item2>Value"`
|
255 |
|
|
Before, After string
|
256 |
|
|
}
|
257 |
|
|
|
258 |
|
|
type PathTestSet struct {
|
259 |
|
|
Item1 []PathTestItem
|
260 |
|
|
}
|
261 |
|
|
|
262 |
|
|
type PathTestD struct {
|
263 |
|
|
Other PathTestSet `xml:"Items"`
|
264 |
|
|
Before, After string
|
265 |
|
|
}
|
266 |
|
|
|
267 |
|
|
type PathTestE struct {
|
268 |
|
|
Underline string `xml:"Items>_>Value"`
|
269 |
|
|
Before, After string
|
270 |
|
|
}
|
271 |
|
|
|
272 |
|
|
var pathTests = []interface{}{
|
273 |
|
|
&PathTestA{Items: []PathTestItem{{"A"}, {"D"}}, Before: "1", After: "2"},
|
274 |
|
|
&PathTestB{Other: []PathTestItem{{"A"}, {"D"}}, Before: "1", After: "2"},
|
275 |
|
|
&PathTestC{Values1: []string{"A", "C", "D"}, Values2: []string{"B"}, Before: "1", After: "2"},
|
276 |
|
|
&PathTestD{Other: PathTestSet{Item1: []PathTestItem{{"A"}, {"D"}}}, Before: "1", After: "2"},
|
277 |
|
|
&PathTestE{Underline: "E", Before: "1", After: "2"},
|
278 |
|
|
}
|
279 |
|
|
|
280 |
|
|
func TestUnmarshalPaths(t *testing.T) {
|
281 |
|
|
for _, pt := range pathTests {
|
282 |
|
|
v := reflect.New(reflect.TypeOf(pt).Elem()).Interface()
|
283 |
|
|
if err := Unmarshal([]byte(pathTestString), v); err != nil {
|
284 |
|
|
t.Fatalf("Unmarshal: %s", err)
|
285 |
|
|
}
|
286 |
|
|
if !reflect.DeepEqual(v, pt) {
|
287 |
|
|
t.Fatalf("have %#v\nwant %#v", v, pt)
|
288 |
|
|
}
|
289 |
|
|
}
|
290 |
|
|
}
|
291 |
|
|
|
292 |
|
|
type BadPathTestA struct {
|
293 |
|
|
First string `xml:"items>item1"`
|
294 |
|
|
Other string `xml:"items>item2"`
|
295 |
|
|
Second string `xml:"items"`
|
296 |
|
|
}
|
297 |
|
|
|
298 |
|
|
type BadPathTestB struct {
|
299 |
|
|
Other string `xml:"items>item2>value"`
|
300 |
|
|
First string `xml:"items>item1"`
|
301 |
|
|
Second string `xml:"items>item1>value"`
|
302 |
|
|
}
|
303 |
|
|
|
304 |
|
|
type BadPathTestC struct {
|
305 |
|
|
First string
|
306 |
|
|
Second string `xml:"First"`
|
307 |
|
|
}
|
308 |
|
|
|
309 |
|
|
type BadPathTestD struct {
|
310 |
|
|
BadPathEmbeddedA
|
311 |
|
|
BadPathEmbeddedB
|
312 |
|
|
}
|
313 |
|
|
|
314 |
|
|
type BadPathEmbeddedA struct {
|
315 |
|
|
First string
|
316 |
|
|
}
|
317 |
|
|
|
318 |
|
|
type BadPathEmbeddedB struct {
|
319 |
|
|
Second string `xml:"First"`
|
320 |
|
|
}
|
321 |
|
|
|
322 |
|
|
var badPathTests = []struct {
|
323 |
|
|
v, e interface{}
|
324 |
|
|
}{
|
325 |
|
|
{&BadPathTestA{}, &TagPathError{reflect.TypeOf(BadPathTestA{}), "First", "items>item1", "Second", "items"}},
|
326 |
|
|
{&BadPathTestB{}, &TagPathError{reflect.TypeOf(BadPathTestB{}), "First", "items>item1", "Second", "items>item1>value"}},
|
327 |
|
|
{&BadPathTestC{}, &TagPathError{reflect.TypeOf(BadPathTestC{}), "First", "", "Second", "First"}},
|
328 |
|
|
{&BadPathTestD{}, &TagPathError{reflect.TypeOf(BadPathTestD{}), "First", "", "Second", "First"}},
|
329 |
|
|
}
|
330 |
|
|
|
331 |
|
|
func TestUnmarshalBadPaths(t *testing.T) {
|
332 |
|
|
for _, tt := range badPathTests {
|
333 |
|
|
err := Unmarshal([]byte(pathTestString), tt.v)
|
334 |
|
|
if !reflect.DeepEqual(err, tt.e) {
|
335 |
|
|
t.Fatalf("Unmarshal with %#v didn't fail properly:\nhave %#v,\nwant %#v", tt.v, err, tt.e)
|
336 |
|
|
}
|
337 |
|
|
}
|
338 |
|
|
}
|
339 |
|
|
|
340 |
|
|
const OK = "OK"
|
341 |
|
|
const withoutNameTypeData = `
|
342 |
|
|
|
343 |
|
|
`
|
344 |
|
|
|
345 |
|
|
type TestThree struct {
|
346 |
|
|
XMLName Name `xml:"Test3"`
|
347 |
|
|
Attr string `xml:",attr"`
|
348 |
|
|
}
|
349 |
|
|
|
350 |
|
|
func TestUnmarshalWithoutNameType(t *testing.T) {
|
351 |
|
|
var x TestThree
|
352 |
|
|
if err := Unmarshal([]byte(withoutNameTypeData), &x); err != nil {
|
353 |
|
|
t.Fatalf("Unmarshal: %s", err)
|
354 |
|
|
}
|
355 |
|
|
if x.Attr != OK {
|
356 |
|
|
t.Fatalf("have %v\nwant %v", x.Attr, OK)
|
357 |
|
|
}
|
358 |
|
|
}
|