示例#1
0
class TornadoHTTPClientCoroutineTest(testing.AsyncTestCase):
    def setUp(self):
        super(TornadoHTTPClientCoroutineTest, self).setUp()
        self.http = TornadoHTTPClient()
        self.http.debug = True

    def _callback(self, response):
        print(response.code)
        print("当前链接地址: ")
        print(response.effective_url)
        self.http.stop()

    @testing.gen_test
    def test_get(self):
        resp = yield self.http.get("http://www.linuxzen.com")
        self._callback(resp)

    @testing.gen_test
    def test_get_args(self):
        yield self.http.get("http://www.baidu.com/s", (("wd", "tornado"),),
                            callback=self._callback)

    @testing.gen_test
    def test_post(self):
        params = [("vimcn", u"# 这是TornadoHTTPClient单元测试提交的".encode("utf-8"))]
        url = "http://p.vim-cn.com"
        def callback(response):
            print("打开此链接:", end=" ")
            print(response.effective_url)

        resp = yield self.http.post(url, params)
        callback(resp)

    @testing.gen_test
    def test_head(self):
        resp = yield self.http.head("http://linuxzen.com")
        self.assertEqual(resp.code, 200)

    @testing.gen_test
    def test_user_agent(self):
        user_agent =  "Mozilla/5.0 (X11; Linux x86_64)"\
                " AppleWebKit/537.11 (KHTML, like Gecko)"\
                " Chrome/23.0.1271.97 Safari/537.11"
        self.http.set_user_agent(user_agent)

        resp = yield self.http.get("http://www.linuxzen.com")
        self.assertEqual(resp.request.user_agent, user_agent)

    @testing.gen_test
    def test_header(self):
        headers = {"Origin":"http://www.linuxzen.com"}
        resp = yield self.http.get("http://www.linuxzen.com", headers=headers)
        self.assertEqual(resp.request.headers["Origin"], headers["Origin"])

    @testing.gen_test
    def test_cookie(self):
        yield self.http.get("http://www.baidu.com")
        print(self.http.cookie)

    @testing.gen_test
    def test_cookie_jar(self):
        yield self.http.get("http://www.baidu.com")
        print("cookie jar>>>>>>>>>>>>>>>>>>", end=" ")
        print(self.http.cookiejar)

    @testing.gen_test
    def test_upload_img(self):
        def callback(response):
            self.http.stop()

        resp = yield self.http.upload("http://dimg.vim-cn.com", "name",
                                        "img_test.png")
        print("打开图片链接", end = " ")
        print(resp.effective_url)

    def test_set_proxy(self):
        self.http.set_proxy('127.0.0.1')