コード例 #1
0
 def test_mapping_response_serialization(self):
     e = MappingResponse(
         additional_proxy_request_headers={"test": "1"},
         base64_body="test2",
         body="test3",
         body_file_name="test4",
         json_body="test5",
         delay_distribution=DelayDistribution(distribution_type="lognormal",
                                              sigma=0.1,
                                              median=0.2),
         fault="test6",
         fixed_delay_milliseconds=500,
         from_configured_stub="test7",
         headers={"test": "1"},
         proxy_base_url="test8",
         status=200,
         status_message="test9",
         transformer_parameters={"test2": "2"},
         transformers=["test10"],
     )
     serialized = e.get_json_data()
     self.assertDictContainsKeyWithValue(serialized,
                                         "additionalProxyRequestHeaders",
                                         {"test": "1"})
     self.assertDictContainsKeyWithValue(serialized, "base64Body", "test2")
     self.assertDictContainsKeyWithValue(serialized, "body", "test3")
     self.assertDictContainsKeyWithValue(serialized, "bodyFileName",
                                         "test4")
     self.assertDictContainsKeyWithValue(serialized, "jsonBody", "test5")
     self.assertDictContainsKeyWithValue(serialized, "delayDistribution", {
         "type": "lognormal",
         "sigma": 0.1,
         "median": 0.2
     })
     self.assertDictContainsKeyWithValue(serialized, "fault", "test6")
     self.assertDictContainsKeyWithValue(serialized,
                                         "fixedDelayMilliseconds", 500)
     self.assertDictContainsKeyWithValue(serialized, "fromConfiguredStub",
                                         "test7")
     self.assertDictContainsKeyWithValue(serialized, "headers",
                                         {"test": "1"})
     self.assertDictContainsKeyWithValue(serialized, "proxyBaseUrl",
                                         "test8")
     self.assertDictContainsKeyWithValue(serialized, "status", 200)
     self.assertDictContainsKeyWithValue(serialized, "statusMessage",
                                         "test9")
     self.assertDictContainsKeyWithValue(serialized,
                                         "transformerParameters",
                                         {"test2": "2"})
     self.assertDictContainsKeyWithValue(serialized, "transformers",
                                         ["test10"])
コード例 #2
0
 def test_mapping_response_serialization(self):
     e = MappingResponse(additional_proxy_request_headers={'test': '1'},
                         base64_body='test2',
                         body='test3',
                         body_file_name='test4',
                         json_body='test5',
                         delay_distribution=DelayDistribution(
                             distribution_type='lognormal',
                             sigma=0.1,
                             median=0.2),
                         fault='test6',
                         fixed_delay_milliseconds=500,
                         from_configured_stub='test7',
                         headers={'test': '1'},
                         proxy_base_url='test8',
                         status=200,
                         status_message='test9',
                         transformer_parameters={'test2': '2'},
                         transformers=['test10'])
     serialized = e.get_json_data()
     self.assertDictContainsKeyWithValue(serialized,
                                         'additionalProxyRequestHeaders',
                                         {'test': '1'})
     self.assertDictContainsKeyWithValue(serialized, 'base64Body', 'test2')
     self.assertDictContainsKeyWithValue(serialized, 'body', 'test3')
     self.assertDictContainsKeyWithValue(serialized, 'bodyFileName',
                                         'test4')
     self.assertDictContainsKeyWithValue(serialized, 'jsonBody', 'test5')
     self.assertDictContainsKeyWithValue(serialized, 'delayDistribution', {
         'type': 'lognormal',
         'sigma': 0.1,
         'median': 0.2
     })
     self.assertDictContainsKeyWithValue(serialized, 'fault', 'test6')
     self.assertDictContainsKeyWithValue(serialized,
                                         'fixedDelayMilliseconds', 500)
     self.assertDictContainsKeyWithValue(serialized, 'fromConfiguredStub',
                                         'test7')
     self.assertDictContainsKeyWithValue(serialized, 'headers',
                                         {'test': '1'})
     self.assertDictContainsKeyWithValue(serialized, 'proxyBaseUrl',
                                         'test8')
     self.assertDictContainsKeyWithValue(serialized, 'status', 200)
     self.assertDictContainsKeyWithValue(serialized, 'statusMessage',
                                         'test9')
     self.assertDictContainsKeyWithValue(serialized,
                                         'transformerParameters',
                                         {'test2': '2'})
     self.assertDictContainsKeyWithValue(serialized, 'transformers',
                                         ['test10'])
コード例 #3
0
 def test_mapping_serialization(self):
     e = Mapping(
         priority=1,
         request=MappingRequest(method="GET", url="test"),
         response=MappingResponse(status=200, status_message="test2"),
         persistent=False,
         post_serve_actions={"test": "1"},
         new_scenario_state="test3",
         required_scenario_state="test4",
         scenario_name="test5",
     )
     serialized = e.get_json_data()
     self.assertDictContainsKeyWithValue(serialized, "priority", 1)
     self.assertDictContainsKeyWithValue(serialized, "request", {
         "method": "GET",
         "url": "test"
     })
     self.assertDictContainsKeyWithValue(serialized, "response", {
         "status": 200,
         "statusMessage": "test2"
     })
     self.assertDictContainsKeyWithValue(serialized, "persistent", False)
     self.assertDictContainsKeyWithValue(serialized, "postServeActions",
                                         {"test": "1"})
     self.assertDictContainsKeyWithValue(serialized, "newScenarioState",
                                         "test3")
     self.assertDictContainsKeyWithValue(serialized,
                                         "requiredScenarioState", "test4")
     self.assertDictContainsKeyWithValue(serialized, "scenarioName",
                                         "test5")
コード例 #4
0
 def test_mapping_serialization(self):
     e = Mapping(priority=1,
                 request=MappingRequest(method='GET', url='test'),
                 response=MappingResponse(status=200,
                                          status_message='test2'),
                 persistent=False,
                 post_serve_actions={'test': '1'},
                 new_scenario_state='test3',
                 required_scenario_state='test4',
                 scenario_name='test5')
     serialized = e.get_json_data()
     self.assertDictContainsKeyWithValue(serialized, 'priority', 1)
     self.assertDictContainsKeyWithValue(serialized, 'request', {
         'method': 'GET',
         'url': 'test'
     })
     self.assertDictContainsKeyWithValue(serialized, 'response', {
         'status': 200,
         'statusMessage': 'test2'
     })
     self.assertDictContainsKeyWithValue(serialized, 'persistent', False)
     self.assertDictContainsKeyWithValue(serialized, 'postServeActions',
                                         {'test': '1'})
     self.assertDictContainsKeyWithValue(serialized, 'newScenarioState',
                                         'test3')
     self.assertDictContainsKeyWithValue(serialized,
                                         'requiredScenarioState', 'test4')
     self.assertDictContainsKeyWithValue(serialized, 'scenarioName',
                                         'test5')
コード例 #5
0
ファイル: resource.py プロジェクト: rapp992/python-wiremock
 def create_mapping(cls, mapping, parameters={}):
     cls.validate_is_entity(mapping, Mapping)
     response = cls.REST_CLIENT.post(cls.get_base_uri(cls.endpoint()),
                                     json=mapping.get_json_data(),
                                     headers=make_headers(),
                                     params=parameters)
     response = cls.REST_CLIENT.handle_response(response)
     return MappingResponse.from_dict(response.json())
コード例 #6
0
def stub_url_options_for_cors(mock_url):
    mapping = Mapping(priority=100,
                      request=MappingRequest(method=HttpMethods.OPTIONS,
                                             url=mock_url),
                      response=MappingResponse(
                          status=200,
                          headers={
                              'Access-Control-Allow-Headers': 'Content-Type',
                              'Access-Control-Allow-Methods': "GET, POST"
                          },
                          body=''),
                      persistent=False)
    Mappings.create_mapping(mapping)
コード例 #7
0
def stub_payment_status(mock_url, mock_json):
    mapping = Mapping(priority=100,
                      request=MappingRequest(method=HttpMethods.GET,
                                             url=mock_url),
                      response=MappingResponse(
                          status=200,
                          headers={
                              'Access-Control-Allow-Headers': 'Content-Type',
                              'Access-Control-Allow-Methods': "GET, POST"
                          },
                          json_body=get_mock_response_from_json(mock_json)),
                      persistent=False)
    Mappings.create_mapping(mapping)
コード例 #8
0
def response(status: int, status_message: str):
    Mappings.create_mapping(mapping=Mapping(
        priority=100,
        request=MappingRequest(
            method=HttpMethods.POST,
            url='/api/v2/logs/ingest',
            headers={'Authorization': {'equalTo': "Api-Token {}".format(ACCESS_KEY)}},
        ),
        response=MappingResponse(
            status=status,
            status_message=status_message
        ),
        persistent=False
    ))
コード例 #9
0
def stub_config(config_json):
    mapping = Mapping(priority=100,
                      request=MappingRequest(
                          method=HttpMethods.GET,
                          url=MockUrl.CONFIG_MOCK_URI.value),
                      response=MappingResponse(
                          status=200,
                          headers={
                              'Access-Control-Allow-Headers': 'Content-Type',
                              'Access-Control-Allow-Methods': "GET, POST"
                          },
                          json_body=get_mock_response_from_json(config_json)),
                      persistent=False)
    Mappings.create_mapping(mapping)
コード例 #10
0
ファイル: wiremock.py プロジェクト: CodeYellowBV/high-templar
def set_bootstrap_response(data):
    Config.base_url = 'http://wiremock:8080/__admin'

    if data is None:
        mapping = Mapping(
            priority=100,
            request=MappingRequest(method=HttpMethods.GET,
                                   url='/api/bootstrap/'),
            response=MappingResponse(status=403, ),
            persistent=False,
        )
    else:
        mapping = Mapping(
            priority=100,
            request=MappingRequest(method=HttpMethods.GET,
                                   url='/api/bootstrap/'),
            response=MappingResponse(
                status=200,
                body=json.dumps(data),
                headers={'Content-Type': 'application/json'}),
            persistent=False,
        )
    Mappings.create_mapping(mapping=mapping)
コード例 #11
0
 def test_mapping_response_deserialization(self):
     serialized = {
         "additionalProxyRequestHeaders": {
             "test": "1"
         },
         "base64Body": "test2",
         "body": "test3",
         "bodyFileName": "test4",
         "jsonBody": "test5",
         "delayDistribution": {
             "type": "lognormal",
             "sigma": 0.1,
             "median": 0.2
         },
         "fault": "test6",
         "fixedDelayMilliseconds": 500,
         "fromConfiguredStub": "test7",
         "headers": {
             "test": "1"
         },
         "proxyBaseUrl": "test8",
         "status": 200,
         "statusMessage": "test9",
         "transformerParameters": {
             "test2": "2"
         },
         "transformers": ["test10"],
     }
     e = MappingResponse.from_dict(serialized)
     self.assertIsInstance(e, MappingResponse)
     self.assertEqual({"test": "1"}, e.additional_proxy_request_headers)
     self.assertEqual("test2", e.base64_body)
     self.assertEqual("test3", e.body)
     self.assertEqual("test4", e.body_file_name)
     self.assertEqual("test5", e.json_body)
     self.assertIsInstance(e.delay_distribution, DelayDistribution)
     self.assertEqual("lognormal", e.delay_distribution.distribution_type)
     self.assertEqual("test6", e.fault)
     self.assertEqual(500, e.fixed_delay_milliseconds)
     self.assertEqual("test7", e.from_configured_stub)
     self.assertEqual({"test": "1"}, e.headers)
     self.assertEqual("test8", e.proxy_base_url)
     self.assertEqual(200, e.status)
     self.assertEqual("test9", e.status_message)
     self.assertEqual({"test2": "2"}, e.transformer_parameters)
     self.assertEqual(["test10"], e.transformers)
コード例 #12
0
 def test_mapping_response_deserialization(self):
     serialized = {
         'additionalProxyRequestHeaders': {
             'test': '1'
         },
         'base64Body': 'test2',
         'body': 'test3',
         'bodyFileName': 'test4',
         'jsonBody': 'test5',
         'delayDistribution': {
             'type': 'lognormal',
             'sigma': 0.1,
             'median': 0.2
         },
         'fault': 'test6',
         'fixedDelayMilliseconds': 500,
         'fromConfiguredStub': 'test7',
         'headers': {
             'test': '1'
         },
         'proxyBaseUrl': 'test8',
         'status': 200,
         'statusMessage': 'test9',
         'transformerParameters': {
             'test2': '2'
         },
         'transformers': ['test10']
     }
     e = MappingResponse.from_dict(serialized)
     self.assertIsInstance(e, MappingResponse)
     self.assertEquals({'test': '1'}, e.additional_proxy_request_headers)
     self.assertEquals('test2', e.base64_body)
     self.assertEquals('test3', e.body)
     self.assertEquals('test4', e.body_file_name)
     self.assertEquals('test5', e.json_body)
     self.assertIsInstance(e.delay_distribution, DelayDistribution)
     self.assertEquals('lognormal', e.delay_distribution.distribution_type)
     self.assertEquals('test6', e.fault)
     self.assertEquals(500, e.fixed_delay_milliseconds)
     self.assertEquals('test7', e.from_configured_stub)
     self.assertEquals({'test': '1'}, e.headers)
     self.assertEquals('test8', e.proxy_base_url)
     self.assertEquals(200, e.status)
     self.assertEquals('test9', e.status_message)
     self.assertEquals({'test2': '2'}, e.transformer_parameters)
     self.assertEquals(['test10'], e.transformers)
コード例 #13
0
def stub_st_request_type_server_error(mock_json, request_type):
    stub_url_options_for_cors(MockUrl.GATEWAY_MOCK_URI.value)
    configure_for_local_host()
    mapping = Mapping(priority=100,
                      request=MappingRequest(
                          method=HttpMethods.POST,
                          url=MockUrl.GATEWAY_MOCK_URI.value,
                          body_patterns=[{
                              "contains": request_type
                          }]),
                      response=MappingResponse(
                          status=500,
                          headers={
                              'Access-Control-Allow-Headers': 'Content-Type',
                              'Access-Control-Allow-Methods': "GET, POST"
                          },
                          json_body=get_mock_response_from_json(mock_json)),
                      persistent=False)
    Mappings.create_mapping(mapping)
コード例 #14
0
def test_labs(app_fixture):
    login_response = requests.post(app_fixture.base_url + '/api/auth/login',
                                   json={
                                       'username': '******',
                                       'password': '******',
                                   })
    assert login_response.ok

    requests.post(app_fixture.base_url + '/api/workers',
                  json={
                      'worker_id': 'TEST_ONE',
                      'host': 'localhost',
                      'port': app_fixture.wiremock_server.port,
                      'state': 1
                  })
    requests.post(app_fixture.base_url + '/api/workers',
                  json={
                      'worker_id': 'TEST_TWO',
                      'host': 'localhost',
                      'port': 7601,
                      'state': 2
                  })
    post_response = requests.post(
        app_fixture.base_url + '/api/labs',
        json={
            'lab_name': 'First lab',
            'lab_type': 'Kathara',
            'start_date': '2020-04-06T14:00:00.000Z',
            'expiration_date': '2020-04-08T14:00:00.000Z',
            'description': 'description',
            'machines': ['m1', 'm2']
        },
        headers={
            'Authorization': 'Bearer {}'.format(login_response.json()['token'])
        })

    assert post_response.ok
    lab_id = post_response.json()
    Config.base_url = 'http://localhost:{}/__admin'.format(
        app_fixture.wiremock_server.port)

    mapping = Mapping(
        priority=100,
        request=MappingRequest(method=HttpMethods.GET,
                               url_path='/labs/status',
                               query_parameters={'lab_ids': {
                                   'matches': '.*'
                               }}),
        response=MappingResponse(status=200,
                                 body=json.dumps([{
                                     'lab_id': lab_id,
                                     'status': 'running'
                                 }])),
        persistent=True,
    )
    Mappings.create_mapping(mapping=mapping)

    response = requests.get(app_fixture.base_url + '/api/labs',
                            headers={
                                'Authorization':
                                'Bearer {}'.format(
                                    login_response.json()['token'])
                            })
    assert response.ok
    body = response.json()
    assert len(body) == 1
    assert body[0]['id'] == lab_id
    assert body[0]['status'] == 'running'