Atlas 0.7.0
Networking protocol for the Worldforge system.
test_negotiation.py
1#test negotiation
2
3#Copyright 2000 by Aloril
4
5#This library is free software; you can redistribute it and/or
6#modify it under the terms of the GNU Lesser General Public
7#License as published by the Free Software Foundation; either
8#version 2.1 of the License, or (at your option) any later version.
9
10#This library is distributed in the hope that it will be useful,
11#but WITHOUT ANY WARRANTY; without even the implied warranty of
12#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13#Lesser General Public License for more details.
14
15#You should have received a copy of the GNU Lesser General Public
16#License along with this library; if not, write to the Free Software
17#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19
20from atlas.transport import negotiation
21import importlib
22importlib.reload(negotiation)
24from atlas import Object
25
26def stop(): import pdb; pdb.set_trace()
27
28def test(input, result="", result_str="", str="", codec=""):
29 res = neg(input)
30 if result!=res: raise ValueError("result: %s" % res)
31 send_str = neg.get_send_str()
32 if result_str!=send_str:
33 raise ValueError("send_str: %s" % send_str)
34 if str!=neg.str: raise ValueError("str: %s" % neg.str)
35 if codec!=neg.selected_codec:
36 raise ValueError("codec: %s" % neg.selected_codec)
37
38#server.....
39neg = NegotiationServer(["Packed", "XML"])
40test("Foo\n", result="fail", result_str="ATLAS server\nHuh?\n\n")
41test("Bar", result="fail", str="Bar")
42assert(neg.other_codecs==[])
43
44#print "-"*60
45neg = NegotiationServer(["Packed", "XML"], id="this is test server")
46test("ATLAS client test here\n", result_str="ATLAS this is test server\n")
47test("ICAN ", str="ICAN ")
48test("Foo", str="ICAN Foo")
49test("\n")
50test("ICAN XML\nICAN Packed\n\n", result="found", result_str="IWILL XML\n\n", codec="XML")
51assert(neg.other_codecs==['Foo', 'XML', 'Packed'])
52
53#print "-"*60
54neg = NegotiationServer(["Packed", "XML"])
55test("ATLAS client test here\nICAN XML\n", codec="XML", result_str="ATLAS server\n")
56test("ICAN Packed\n\n", result="found", result_str="IWILL XML\n\n", codec="XML")
57assert(neg.other_codecs==['XML', 'Packed'])
58
59#print "-"*60
60neg = NegotiationServer(["Packed", "XML"])
61test("", result_str="ATLAS server\n")
62test("ATLAS client test here\nICAN XML\n", codec="XML")
63test("ICAN Packed\n\n", result="found", result_str="IWILL XML\n\n", codec="XML")
64assert(neg.other_codecs==['XML', 'Packed'])
65
66#print "-"*60
67neg = NegotiationServer(["Packed", "XML"])
68test("ATLAS client test here\nICAN X-foo\n", result_str="ATLAS server\n")
69test("\n", result="fail", result_str="ICAN Packed\nICAN XML\n\n")
70assert(neg.other_codecs==['X-foo'])
71
72#client.....
73#print "-"*60
74neg = NegotiationClient(["Packed", "XML"])
75test("", result_str="ATLAS client\n")
76test("ATLAS server hi there\n", result_str="ICAN Packed\nICAN XML\n\n")
77test("IWILL Packed\n\n", result="found", codec="Packed")
78assert(neg.other_codecs==['Packed'])
79
80#client.....
81#print "-"*60
82neg = NegotiationClient(["X-foo"])
83test("", result_str="ATLAS client\n")
84test("ATLAS server hi there\n", result_str="ICAN X-foo\n\n")
85test("ICAN Packed\nICAN XML\n\n", result="fail")
86assert(neg.other_codecs==['Packed', 'XML'])
87
88neg = NegotiationClient()
89test("ATLAS server hi there\n", result_str="ATLAS client\nICAN Bach_beta2\nICAN XML\nICAN XML2_test\nICAN Packed\nICAN Binary1_beta\nICAN Binary2_test\n\n")
90test("IWILL XML\n\n", result="found", codec="XML")
91co = neg.get_codec()
92s = co.encode(Object(foo=42))+co.encoder.close()
93assert(s == '<atlas>\n\t<map>\n\t\t<int name="foo">42</int>\n\t</map>\n</atlas>\n')
94assert(co.decode("<atlas>")==[])
95assert(co.decode(s) == [Object(foo=42)])
Definition: test.py:1