1#!/usr/bin/env python
2#
3# spyne - Copyright (C) Spyne contributors.
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
18#
19
20import unittest
21
22import time
23
24import pytz
25
26from datetime import datetime
27
28from spyne.test.interop._test_soap_client_base import server_started
29from spyne.util import thread, urlencode, urlopen, Request, HTTPError
30
31
32_server_started = False
33
34
35class TestHttpRpc(unittest.TestCase):
36    def setUp(self):
37        global _server_started
38        from spyne.test.interop.server.httprpc_pod_basic import main, port
39
40        if not _server_started:
41            def run_server():
42                main()
43
44            thread.start_new_thread(run_server, ())
45
46            # FIXME: Does anybody have a better idea?
47            time.sleep(2)
48
49            _server_started = True
50
51        self.base_url = 'http://localhost:%d' % port[0]
52
53    def test_404(self):
54        url = '%s/404' % self.base_url
55        try:
56            data = urlopen(url).read()
57        except HTTPError as e:
58            assert e.code == 404
59
60    def test_413(self):
61        url = self.base_url
62        try:
63            data = Request(url,("foo"*3*1024*1024))
64        except HTTPError as e:
65            assert e.code == 413
66
67    def test_500(self):
68        url = '%s/python_exception' % self.base_url
69        try:
70            data = urlopen(url).read()
71        except HTTPError as e:
72            assert e.code == 500
73
74    def test_500_2(self):
75        url = '%s/soap_exception' % self.base_url
76        try:
77            data = urlopen(url).read()
78        except HTTPError as e:
79            assert e.code == 500
80
81    def test_echo_string(self):
82        url = '%s/echo_string?s=punk' % self.base_url
83        data = urlopen(url).read()
84
85        assert data == b'punk'
86
87    def test_echo_integer(self):
88        url = '%s/echo_integer?i=444' % self.base_url
89        data = urlopen(url).read()
90
91        assert data == b'444'
92
93    def test_echo_datetime(self):
94        dt = datetime.now(pytz.utc).isoformat().encode('ascii')
95        params = urlencode({
96            'dt': dt,
97        })
98
99        print(params)
100        url = '%s/echo_datetime?%s' % (self.base_url, str(params))
101        data = urlopen(url).read()
102
103        assert dt == data
104
105    def test_echo_datetime_tz(self):
106        dt = datetime.now(pytz.utc).isoformat().encode('ascii')
107        params = urlencode({
108            'dt': dt,
109        })
110
111        print(params)
112        url = '%s/echo_datetime?%s' % (self.base_url, str(params))
113        data = urlopen(url).read()
114
115        assert dt == data
116
117
118if __name__ == '__main__':
119    unittest.main()
120