restFixture - I'd like a assert a simple text response - automated-tests

This should be a pretty simple thing, but for some reason I am struggling to find an example anywhere.
I have been using the awesome restFixture for some time, and all my assertions are on json content that is returned.
We now have a microservice that is returning a pure text response (so not json, it's actually a csv). I am struggling to find out how to assert the pure text response!
I've used this before my GET request:
|Table:smartrics.rest.fitnesse.fixture.RestFixtureConfig|
|restfixture.content.handlers.map |text/plain=TEXT |
...but I am not sure how to assert the response.
The message I get back is:
java.lang.IllegalArgumentException: Cannot evaluate 'TOKEN,CARD_LOGO,TRANSACTION_DATE' in TOKEN,CARD_LOGO,TRANSACTION_DATE
My table looks like this:
!3 GET Report
|Table:smartrics.rest.fitnesse.fixture.RestFixture|${ReportServiceEndPoint} |
|setHeader |${ReportServiceHeader} | |
|GET |?transactionDateFrom=${myTransactionDateFrom}&transactionDateTo=${myTransactionDateTo}&requestorId=${myRequestorId} |${myResponseCode}||${myExpectedResultThatOnlyChecksHeaders}|
Any help would be greatly appreciated, as I will soon have to do more complex assertions on this text response.

I don't know how/whether it can be done by restFixture, but my HttpTest fixture allows you to capture any response (and perform a check on the full content, possibly using Slim's regular expression support). Alternatively you can place the response in a Slim variable and then do assertions by applying regular expressions using StringFixture.
I suppose restFixture also allows access to the 'raw' response. So this should be possible there also.

Related

Symfony PHPUnit integration test not able to check full body text

My use case is simple: I am sending text emails, i am trying to make integrations tests in order to check the full text body of the message, please note I don't want to check if message contain a string, i am looking for format and structure. No fancy check since it is just text.
The current public API, as in documentation, and as I see in code allows me to check only whether the message contains a string assertEmailTextBodyContains().
I did read: https://symfony.com/doc/current/mailer.html#write-a-functional-test, from MailerAssertionsTrait can only get a RawMessage, i tried, but did not get a strait way to wrap it within an Email.
What am I missing?
Is there any technical/non technical issue preventing to have such Constraint implemented?
Something like: assertEmailTextBodySameAs() for example.
Just for the sake of documentation.
After informing my self more, i realise that in my ignorance i was looking for an idiomatic syntax in the MailerAssertionsTrait instead what i needed was just to use the IsIdentical constraint from phpunit.
$this->assertThat($email->getTextBody(), new IsIdentical($expectedText), 'Mail body are not the same');
Why such assertion is not built in in trait i did assume it is just to keep it simple allowing others like me to extend later on easily, it is just an speculation though.

How to Write Response Assertion Using Groovy in JMeter

I want to write a code for Response assertion using groovy for one of the Request Giving Response data like this
{
"value":"200"
"value_description":"pass"
"value_code":"pass"
"data_encode":"uyt-09-0nbv"
}
after google Search i am getting only with Response Assertion SOAP-UI tools and i also checked with Blaze meter blog i am not understating about what they are saying. simple way i want demonstrate that.write code for Response Data Assert value for 200 is this possible. please help me to this stuff
The relevant Groovy code to check whether value attribute in the response equals 200 would be something like:
def json = new groovy.json.JsonSlurper().parse(prev.getResponseData())
def value = json.value
if (value != '200') {
AssertionResult.setFailure(true)
AssertionResult.setFauilreMessage('Expected 200, but got ' + value)
}
Add JSR223 Assertion as a child of the request which returns the aforementioned JSON (it is not valid JSON by the way)
Put the above code into "Script" area (make sure you tick Cache compiled script if available box and choose groovy from the "Language" dropdown)
More information:
Groovy: Parsing and producing JSON
Scripting JMeter Assertions in Groovy - A Tutorial
You can add 4 JSON Extractors, each with different Path Expressions:
$.value, $.value_description, $.value_code, $.data_encode
It will assert that JSON parameter returned.
You can add Regular Expression Extractors to check each variable you got using Apply to JMeter Variable.

Nesting HTTP GET parameters (request within a request)

I want to call a JSP with GET parameters within the GET parameter of a parent JSP. The URL for this would be http://server/getMap.jsp?lat=30&lon=-90&name=http://server/getName.jsp?lat1=30&lon1=-90
getName.jsp will return a string that goes in the name parameter of getMap.jsp.
I think the problem here is that &lon1=-90 at the end of the URL will be given to getMap.jsp instead of getName.jsp. Is there a way to distinguish which GET parameter goes to which URL?
One idea I had was to encode the second URL (e.g. = -> %3D and & -> %26) but that didn't work out well. My best idea so far is to allow only one parameter in the second URL, comma-delimited. So I'll have http://server/getMap.jsp?lat=30&lon=-90&name=http://server/getName.jsp?params=30,-90 and leave it up to getName.jsp to parse its variables. This way I leave the & alone.
NOTE - I know I can approach this problem from a completely different angle and avoid nested URLs altogether, but I still wonder (for the sake of knowledge!) if this is possible or if anyone has done it...
This has been done a lot, especially with ad serving technologies and URL redirects
But an encoded URL should just work fine. You need to completely encode it tho. A generator can be found here
So this:
http://server/getMap.jsp?lat=30&lon=-90&name=http://server/getName.jsp?lat1=30&lon1=-90
becomes this: http://server/getMap.jsp?lat=30&lon=-90&name=http%3A%2F%2Fserver%2FgetName.jsp%3Flat1%3D30%26lon1%3D-90
I am sure that jsp has a function for this. Look for "urlencode". Your JSP will see the contents of the GET-Variable "name" as the unencoded string: "http://server/getName.jsp?lat1=30&lon1=-90"

Is it considered bad practice to perform HTTP POST without entity body?

I need to invoke a process which doesn't require any input from the user, just a trigger. I plan to use POST /uri without a body to trigger the process. I want to know if this is considered bad from both HTTP and REST perspectives?
I asked this question on the IETF HTTP working group a few months ago. The short answer is: NO, it's not a bad practice (but I suggest reading the thread for more details).
Using a POST instead of a GET is perfectly reasonable, since it also instructs the server (and gateways along the way) not to return a cached response.
POST is completely OK. In difference of GET with POST you are changing the state of the system (most likely your trigger is "doing" something and changing data).
I used POST already without payload and it "feels" OK. One thing you should do when using POST without payload: Pass header Content-Length: 0. I remember problems with some proxies when I api-client didn't pass it.
If you use POST /uri without a body it is something like using a function which does not take an argument .e.g int post (void); so it is reasonable to have function to your resource class which can change the state of an object without having an argument. If you consider to implement the Unix touch function for a URI, is not it be good choice?
Yes, it's OK to send a POST request without a body and instead use query string parameters. But be careful if your parameters contain characters that are not HTTP valid you will have to encode them.
For example if you need to POST 'hello world' to and end point you would have to make it look like this: http://api.com?param=hello%20world
Support for the answers that POST is OK in this case is that in Python's case, the OpenAPI framework "FastAPI" generates a Swagger GUI (see image) that doesn't contain a Body section when a method (see example below) doesn't have a parameter to accept a body.
the method "post_disable_db" just accepts a path parameter "db_name" and doesn't have a 2nd parameter which would imply a mandatory body.
#router.post('/{db_name}/disable',
status_code=HTTP_200_OK,
response_model=ResponseSuccess,
summary='',
description=''
)
async def post_disable_db(db_name: str):
try:
response: ResponseSuccess = Handlers.databases_handler.post_change_db_enabled_state(db_name, False)
except HTTPException as e:
raise (e)
except Exception as e:
logger.exception(f'Changing state of DB to enabled=False failed due to: {e.__repr__()}')
raise HTTPException(HTTP_500_INTERNAL_SERVER_ERROR, detail=e.__repr__())
return response

HTTPModule filter questions

I have one issues I'm struggling with with regards to my HTTPModule filter:
1) I notice that the module gets it's data in chunks. This is problematic for me because I'm using a regex to find and replace. If I get a partial match in one chunk and the rest of the match in the second, it will not work. Is there any way to get the entire response before I do my thing to it? I have seen code where it appends data to a string builder until it uses a matches on an "" end tag but my code must work for more that just (xml, custom tags, etc). I don't know how to detect the End Of Stream or if that is even possible.
I am attaching the filter in the BeginRequest.
Have a look at this example. It looks for "" in the stream of the page.
Here's a sample project which performs buffered search and replace in an HttpModule using a Request.Filter and Response.Filter. You should be able to adapt this technique to perform a Regex easily.
https://github.com/snives/HttpModuleRewrite

Resources