class InterfaceModel():
    def __init__(self):
        LogDefine()
        self.http_client = SynchronousHttpClient()
        # self.host
        # self.port
        # self.method
        # self.parameters
        # self.data
        self.verification = []

    def define_request_method(self, method, url, parameters=None, data=None):
        """请求模板"""
        try:
            res = self.http_client.request(method=method,
                                           url=url,
                                           params=parameters,
                                           data=data)
            http_code = res.status_code
            if http_code == HTTP_CODE_SUCCESS:
                logging.info("if code == 200 ,request success!")
                print u"返回200,请求成功", res.url
                print res.text
                return res

            else:
                logging.error("not return code 200 ,request hava some problem")
                print res.url
                print u"响应出错 code %s" % http_code
                print res.text
        except Exception, e:
            logging.info(u"请求出问题了 %s ", e)
            print e
Ejemplo n.º 2
0
    def test_simple_get(self):
        httpretty.register_uri(
            httpretty.GET, "http://swagger.py/client-test",
            body='expected')

        uut = SynchronousHttpClient()
        resp = uut.request('GET', "http://swagger.py/client-test",
                           params={'foo': 'bar'})
        self.assertEqual(200, resp.status_code)
        self.assertEqual('expected', resp.text)
        self.assertEqual({'foo': ['bar']},
                         httpretty.last_request().querystring)
Ejemplo n.º 3
0
    def test_simple_get(self):
        httpretty.register_uri(httpretty.GET,
                               "http://swagger.py/client-test",
                               body='expected')

        uut = SynchronousHttpClient()
        resp = uut.request('GET',
                           "http://swagger.py/client-test",
                           params={'foo': 'bar'})
        self.assertEqual(200, resp.status_code)
        self.assertEqual('expected', resp.text)
        self.assertEqual({'foo': ['bar']},
                         httpretty.last_request().querystring)
Ejemplo n.º 4
0
    def test_auth_leak(self):
        httpretty.register_uri(httpretty.GET,
                               "http://hackerz.py",
                               body='expected')

        uut = SynchronousHttpClient()
        uut.set_basic_auth("swagger.py", 'unit', 'peekaboo')
        resp = uut.request('GET', "http://hackerz.py", params={'foo': 'bar'})
        self.assertEqual(200, resp.status_code)
        self.assertEqual('expected', resp.text)
        self.assertEqual({'foo': ['bar']},
                         httpretty.last_request().querystring)
        self.assertTrue(
            httpretty.last_request().headers.get('Authorization') is None)
Ejemplo n.º 5
0
    def test_api_key(self):
        httpretty.register_uri(
            httpretty.GET, "http://swagger.py/client-test",
            body='expected')

        uut = SynchronousHttpClient()
        uut.set_api_key("swagger.py",
                        'abc123', param_name='test')
        resp = uut.request('GET', "http://swagger.py/client-test",
                           params={'foo': 'bar'})
        self.assertEqual(200, resp.status_code)
        self.assertEqual('expected', resp.text)
        self.assertEqual({'foo': ['bar'], 'test': ['abc123']},
                         httpretty.last_request().querystring)
Ejemplo n.º 6
0
    def test_real_post(self):
        httpretty.register_uri(
            httpretty.POST, "http://swagger.py/client-test",
            body='expected', content_type='text/json')

        uut = SynchronousHttpClient()
        resp = uut.request('POST', "http://swagger.py/client-test",
                           data={'foo': 'bar'})
        self.assertEqual(200, resp.status_code)
        self.assertEqual('expected', resp.text)
        self.assertEqual('application/x-www-form-urlencoded',
                         httpretty.last_request().headers['content-type'])
        self.assertEqual("foo=bar",
                         httpretty.last_request().body)
def make_request(method, url, parameters=None, data=None):
    http_client = SynchronousHttpClient()
    """
    method = 'get'
    parameters = {
        'keywords': '刘德华',
    }
    参数根据swagger定义的参数进行传递
    """
    z = http_client.request(
        method=method, url=url, params=parameters,
        data=data)  #Request请求传入method= 'get'|'post'|'put' 等,传入url 传入请求参数
    #http_client.websockets.difference()
    return z
Ejemplo n.º 8
0
    def test_real_post(self):
        httpretty.register_uri(httpretty.POST,
                               "http://swagger.py/client-test",
                               body='expected',
                               content_type='text/json')

        uut = SynchronousHttpClient()
        resp = uut.request('POST',
                           "http://swagger.py/client-test",
                           data={'foo': 'bar'})
        self.assertEqual(200, resp.status_code)
        self.assertEqual('expected', resp.text)
        self.assertEqual('application/x-www-form-urlencoded',
                         httpretty.last_request().headers['content-type'])
        self.assertEqual("foo=bar", httpretty.last_request().body)
Ejemplo n.º 9
0
    def test_auth_leak(self):
        httpretty.register_uri(
            httpretty.GET, "http://hackerz.py",
            body='expected')

        uut = SynchronousHttpClient()
        uut.set_basic_auth("swagger.py", 'unit', 'peekaboo')
        resp = uut.request('GET', "http://hackerz.py",
                           params={'foo': 'bar'})
        self.assertEqual(200, resp.status_code)
        self.assertEqual('expected', resp.text)
        self.assertEqual({'foo': ['bar']},
                         httpretty.last_request().querystring)
        self.assertTrue(
            httpretty.last_request().headers.get('Authorization') is None)
Ejemplo n.º 10
0
    def test_basic_auth(self):
        httpretty.register_uri(
            httpretty.GET, "http://swagger.py/client-test",
            body='expected')

        uut = SynchronousHttpClient()
        uut.set_basic_auth("swagger.py", 'unit', 'peekaboo')
        resp = uut.request('GET', "http://swagger.py/client-test",
                           params={'foo': 'bar'})
        self.assertEqual(200, resp.status_code)
        self.assertEqual('expected', resp.text)
        self.assertEqual({'foo': ['bar']},
                         httpretty.last_request().querystring)
        self.assertEqual('Basic %s' % base64.b64encode("unit:peekaboo"),
                         httpretty.last_request().headers.get('Authorization'))
Ejemplo n.º 11
0
    def test_basic_auth(self):
        httpretty.register_uri(httpretty.GET,
                               "http://swagger.py/client-test",
                               body='expected')

        uut = SynchronousHttpClient()
        uut.set_basic_auth("swagger.py", 'unit', 'peekaboo')
        resp = uut.request('GET',
                           "http://swagger.py/client-test",
                           params={'foo': 'bar'})
        self.assertEqual(200, resp.status_code)
        self.assertEqual('expected', resp.text)
        self.assertEqual({'foo': ['bar']},
                         httpretty.last_request().querystring)
        self.assertEqual('Basic %s' % base64.b64encode("unit:peekaboo"),
                         httpretty.last_request().headers.get('Authorization'))
Ejemplo n.º 12
0
    def test_api_key(self):
        httpretty.register_uri(httpretty.GET,
                               "http://swagger.py/client-test",
                               body='expected')

        uut = SynchronousHttpClient()
        uut.set_api_key("swagger.py", 'abc123', param_name='test')
        resp = uut.request('GET',
                           "http://swagger.py/client-test",
                           params={'foo': 'bar'})
        self.assertEqual(200, resp.status_code)
        self.assertEqual('expected', resp.text)
        self.assertEqual({
            'foo': ['bar'],
            'test': ['abc123']
        },
                         httpretty.last_request().querystring)