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 
20 from atlas.transport import negotiation
21 import importlib
22 importlib.reload(negotiation)
23 from atlas.transport.negotiation import *
24 from atlas import Object
25 
26 def stop(): import pdb; pdb.set_trace()
27 
28 def 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.....
39 neg = NegotiationServer(["Packed", "XML"])
40 test("Foo\n", result="fail", result_str="ATLAS server\nHuh?\n\n")
41 test("Bar", result="fail", str="Bar")
42 assert(neg.other_codecs==[])
43 
44 #print "-"*60
45 neg = NegotiationServer(["Packed", "XML"], id="this is test server")
46 test("ATLAS client test here\n", result_str="ATLAS this is test server\n")
47 test("ICAN ", str="ICAN ")
48 test("Foo", str="ICAN Foo")
49 test("\n")
50 test("ICAN XML\nICAN Packed\n\n", result="found", result_str="IWILL XML\n\n", codec="XML")
51 assert(neg.other_codecs==['Foo', 'XML', 'Packed'])
52 
53 #print "-"*60
54 neg = NegotiationServer(["Packed", "XML"])
55 test("ATLAS client test here\nICAN XML\n", codec="XML", result_str="ATLAS server\n")
56 test("ICAN Packed\n\n", result="found", result_str="IWILL XML\n\n", codec="XML")
57 assert(neg.other_codecs==['XML', 'Packed'])
58 
59 #print "-"*60
60 neg = NegotiationServer(["Packed", "XML"])
61 test("", result_str="ATLAS server\n")
62 test("ATLAS client test here\nICAN XML\n", codec="XML")
63 test("ICAN Packed\n\n", result="found", result_str="IWILL XML\n\n", codec="XML")
64 assert(neg.other_codecs==['XML', 'Packed'])
65 
66 #print "-"*60
67 neg = NegotiationServer(["Packed", "XML"])
68 test("ATLAS client test here\nICAN X-foo\n", result_str="ATLAS server\n")
69 test("\n", result="fail", result_str="ICAN Packed\nICAN XML\n\n")
70 assert(neg.other_codecs==['X-foo'])
71 
72 #client.....
73 #print "-"*60
74 neg = NegotiationClient(["Packed", "XML"])
75 test("", result_str="ATLAS client\n")
76 test("ATLAS server hi there\n", result_str="ICAN Packed\nICAN XML\n\n")
77 test("IWILL Packed\n\n", result="found", codec="Packed")
78 assert(neg.other_codecs==['Packed'])
79 
80 #client.....
81 #print "-"*60
82 neg = NegotiationClient(["X-foo"])
83 test("", result_str="ATLAS client\n")
84 test("ATLAS server hi there\n", result_str="ICAN X-foo\n\n")
85 test("ICAN Packed\nICAN XML\n\n", result="fail")
86 assert(neg.other_codecs==['Packed', 'XML'])
87 
88 neg = NegotiationClient()
89 test("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")
90 test("IWILL XML\n\n", result="found", codec="XML")
91 co = neg.get_codec()
92 s = co.encode(Object(foo=42))+co.encoder.close()
93 assert(s == '<atlas>\n\t<map>\n\t\t<int name="foo">42</int>\n\t</map>\n</atlas>\n')
94 assert(co.decode("<atlas>")==[])
95 assert(co.decode(s) == [Object(foo=42)])
Definition: test.py:1