How to use assertResponseHeaders method in Django Test Plus

Best Python code snippet using django-test-plus_python

webservices_assertions.py

Source:webservices_assertions.py Github

copy

Full Screen

...45 This will be used for asseting response code and response full status46 """47 test.assertEqual(response.status, code)48 test.assertEqual(response.full_status, RESPONSE_STATUS_MAP[code])49def assertResponseHeaders(test, response, headerType="json"):50 """51 Asserts response headers for API call, currently looking for Content-Type52 test : This parameter expects handle for current testcase under execution.53 This is used for further assertions54 response : response received from htm.it webservice call55 headerType : Expects string . Currently supporting header type for json and56 html. Update this utility for supporting other headers if needs.57 Defauls to json as with majority we are returning json58 data(also backward compatibity with integration tests that are in prodction)59 """60 headers = dict(response.headers)61 if headerType == "json":62 test.assertEqual(headers["Content-Type"], "application/json; charset=UTF-8")63 elif headerType == "html":64 test.assertEqual(headers["Content-Type"], "text/html")65 else:66 test.fail("Unexpected headerType=%r" % (headerType,))67def assertResponseBody(test, response):68 """69 Asserts response body for API call70 test : This parameter expects handle for current testcase under execution.71 This is used for further assertions72 response : response received from htm.it webservice call73 """74 test.assertIsInstance(response.body, types.StringTypes)75 headers = dict(response.headers)76 if headers["Content-Type"] == "application/json; charset=UTF-8":77 json.loads(response.body)78def assertSuccess(test, response, code=200):79 """80 Asserts response for API call, for 200 OK,81 This is wrapper for asserting happy path testcases82 test : This parameter expects handle for current testcase under execution.83 This is used for further assertions84 response : response received from htm.it webservice call85 code :Defautls to 200, this is the expected response code.This will86 be used for asserting response code and response full status87 """88 assertResponseHeaders(test, response, headerType="json")89 assertResponseBody(test, response)90 assertResponseStatusCode(test, response, code)91def assertNotFound(test, response, headerType="html"):92 """93 Asserts response for API call, for 404 Not Found94 This is wrapper which is useful to assert Not Found scenario across APIs95 test : This parameter expects handle for current testcase under execution.96 This is used for further assertions97 response : response received from htm.it webservice call98 """99 assertResponseHeaders(test, response, headerType)100 assertResponseBody(test, response)101 assertResponseStatusCode(test, response, code=404)102def assertBadRequest(test, response, headerType="html"):103 """104 Asserts response for API call, for 400 Bad Request105 This is wrapper which is useful to assert Bad Request scenario across APIs106 test : This parameter expects handle for current testcase under execution.107 This is used for further assertions108 response : response received from htm.it webservice call109 headerType : Optional, will default to html but can be manually overridden as "json"110 if this is what file type we expect.111 """112 assertResponseHeaders(test, response, headerType)113 assertResponseBody(test, response)114 assertResponseStatusCode(test, response, code=400)115def assertDeleteSuccessResponse(test, response):116 """117 This method wraps all assertions for any successful Delete call118 This could be used for model, instances etc119 test : This parameter expects handle for current testcase under execution.120 This is used for further assertions121 response : response received from htm.it webservice call122 """123 assertSuccess(test, response)124 result = app_utils.jsonDecode(response.body)125 test.assertIsInstance(result, dict)126 test.assertEqual(result, {"result": "success"})127def assertForbiddenResponse(test, response):128 """129 This method wraps all assertions for 403 Forbidden130 test : This parameter expects handle for current testcase under execution.131 This is used for further assertions132 response : response received from htm.it webservice call133 """134 assertResponseHeaders(test, response, headerType="json")135 assertResponseBody(test, response)136 assertResponseStatusCode(test, response, code=403)137def assertInvalidAuthenticationResponse(test, response):138 """139 This method wraps all assertions for Authentication.140 Currently we are returning "400 Bad Request" instead of "401 Unauthorized"141 as a design decision142 test : This parameter expects handle for current testcase under execution.143 This is used for further assertions144 response : response received from htm.it webservice call145 """146 assertResponseHeaders(test, response)147 assertResponseBody(test, response)148 assertResponseStatusCode(test, response, code=400)149 result = app_utils.jsonDecode(response.body)150 test.assertIsInstance(result, dict)151 test.assertEqual(result, {"result": "Invalid API Key"})152def assertMethodNotAllowed(test, response):153 """154 This method wraps all assertions 405 Method Not Allowed155 test : This parameter expects handle for current testcase under execution.156 This is used for further assertions157 response : response received from htm.it webservice call158 """159 assertResponseHeaders(test, response, headerType="html")160 assertResponseBody(test, response)161 assertResponseStatusCode(test, response, code=405)162def assertRouteNotFound(test, response):163 """164 This method wraps all assertions for scenarop when invalid route is invoked165 test : This parameter expects handle for current testcase under execution.166 This is used for further assertions167 response : response received from htm.it webservice call168 """169 assertNotFound(test, response)170 test.assertIn("not found", response.body)171def assertInternalServerError(test, response):172 """173 This method wraps all assertions 500 internal server error174 test : This parameter expects handle for current testcase under execution.175 This is used for further assertions176 response : response received from htm.it webservice call177 """178 assertResponseHeaders(test, response, headerType="html")179 assertResponseStatusCode(test, response, code=500)180def assertObjectNotFoundError(test, response):181 """182 This method wraps all assertions 404 ObjectNotFoundError183 test : This parameter expects handle for current testcase under execution.184 This is used for further assertions185 response : response received from htm.it webservice call186 """187 assertResponseHeaders(test, response, headerType="html")188 assertResponseStatusCode(test, response, code=404)189 test.assertIn("ObjectNotFoundError", response.body)190def assertInvalidArgumentsError(test, response, headerType="html"):191 """192 This method wraps all assertions 500 InvalidArgumentsError()193 test : This parameter expects handle for current testcase under execution.194 This is used for further assertions195 response : response received from htm.it webservice call196 headerType : Optional, will default to html but can be manually overridden as "json"197 if this is what file type we expect.198 """199 assertResponseHeaders(test, response, headerType)200 assertResponseStatusCode(test, response, code=400)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Django Test Plus automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful