I am testing an API call to the server using Cucumber + Capybara with Selenium WebDriver.
I managed to get the response obj, but how do you assert that the response body contains
certain String? For example if the response body contains "Hello World" I want to assert that
this response body (Which is string) contains a pattern "World"
ex. Something like:
response = http.request(request)
response.body.should
have_text("World")
Alternatively is there a way to get "application/json" from the response and assert the contents using
Capybara?
Thanks!
I think what your looking for is have_content.
response.body.should have_content("World")
For more info, check out the README: Check out the documentation: https://github.com/jnicklas/capybara
Related
What I want to do is return custom headers in hyper (but return them and return the response body as well) For example, I'm going to take the code from the hyper documentation as an example:
async fn handle(_: Request<Body>) -> Result<Response<Body>, Infallible> {
Ok(Response::new("Hello, World!".into()))
}
For example, this code only displays Hello World! on every request, and always returns a 200 status code. But for example, How can I send other types of custom headers? What I tried was to use Response::builder instead of Response::new (as you can see in my example I use ::new, what I tried separately was to use ::builder) but it gave errors since that type of data cannot be returned. So how can I return the header I want and as many as I want but keeping the "body"?
In general your idea of using Response::builder seems to be correct. Note however that it returns a Builder, which method body must be later used to create a Response. As body's documentation states:
“Consumes” this builder, using the provided body to return a constructed Response.
A working example of setting custom headers of a response (how many you like) you can see in the documentation of Builder::header. It looks like this:
let body = "Hello, world!";
let response = Response::builder()
.header("Content-Type", "text/html")
.header("X-Custom-Foo", "bar")
.header("content-length", body.len())
.body(body.into())
.unwrap();
First question:
I have a API GET request which contains path parameters, query parameters and headers, and I'd like to put my request url as a string variable, how can I achieve it?
Second quesiton:
How to pass pathParams to a string variable?
I've studied how to pass path parameters, but all examples get("http://some_url/{path}"), I'd like to put the url as a String.
like String url = "http://my/request/url",
how to get with url+{id}? not get the http string?
given()
.contentType(ContentTypeJSON).
with()
.pathParams("id", "1").
when()
.get("http://my/request/url/{id}").
then()
.assertThat().
.statusLine("HTTP/1.1 200 OK");
Simple way to avoid rework is using the below code .
/*
* We can parameterize it using baseURI and basePath and send a request to get a customer using ID
*/
RestAssured.baseURI = "http://parabank.parasoft.com/";
RestAssured.basePath = "parabank/services/bank/customers";
//also we can use a path parameter for the same request
given().contentType(ContentType.JSON).pathParam("customers", "12212").when().get("{customers}/").then().statusCode(200).log().all();
I have to send array of ids in GET request as paramenter.How can I test it in Postman(google chrome extension for API testing)?
The scenario is I have url, www.something.com/activity/poi_ids
poi_ids should conatain arrays of ids like [316,318]
At api side using express,
app.get('/activity/:poi_ids',function(req,res){
var poi_ids = req.params.poi_ids;
.......
.......
});
I have looked into it but it is only for post request
you can send them via query params ..
in your http query params assign all values to same variables like
GET activity?pid=12&pid=23&pid=34
and then inside your express get it like
var ids=req.query.pid; //[12,23,34]
It is unstructured text. If you want to "send an array" then you'll need to design some way to encode it and then write JavaScript to decode it.
For example:
GET /activity/316-318
and
var poi_ids = req.params.poi_ids.split("-");
I'm trying to log the body of a post made using org.apache.http.client. I'm using Scalatra version 2.4.0.RC3 and Scala version 2.11.7. My response is a 400 Bad Request and I need to get the message provided in the response body.
Here is my current code:
val response = client.execute(post)
println(response)
println(response.getEntity().getContent())
response.getEntity().getContent() prints:
java.io.ByteArrayInputStream#1q232e4e
I need to get the actual body as string from this ByteArrayInputStream.
You can use EntityUtils form the same library:
import org.apache.http.util.EntityUtils;
println(EntityUtils.toString(response.getEntity()));
I'm new to using groovy and have started to use it to test some REST services. I'm having an issue parsing my XML response from our service due to 'Content not allowed in prolog.' After awhile searching I came across a post saying there might be a Byte Order Marker at the beginning. To compensate I followed their approach to trim the characters before the first < and then parse the response. While this works, I was also told the issue is that the response is coming back as 'Transfer-Encoding: chunked'.
Using HTTPBuilder, is there a way to handle chunked responses without trimming characters off?
If I try:
def http = new HTTPBuilder('url')
http.request( Method.valueOf("GET"), XML )
I get the 'Content not allowed in prolog message. However:
http.request( Method.valueOf("GET"), TEXT )
Works, but requires trimming the text until the first < before sending the response to XmlParser.
I had the same issue when I needed to interact with an IIS server. The XML returned had a bogus character in front of the actual XML returned by the web server. I worked around it like this:
StringReader reader = builder.get( path: 'rcserver/systeminfo.xml', contentType: ContentType.TEXT )
def text = reader.getText()
def xml = new XmlSlurper().parseText(text.substring(1));
The HTTPBuilder class has a setContentEncoding() method that allows you to specify the response's content-type.
Maybe something like:
http.contentEncoding = ContentEncoding.Type.GZIP
http.request( Method.GET, XML)
Hope this helps.
I was having this problem as well hitting an IIS server over https. Here is a little addition to Wim Deblauwe's answer for a POST request. You have to send a different type in the request than you expect in the response.
Send a POST with XML as the request type and TEXT as the response type. Then, parse the text response into XML. This worked for me.
In Groovy:
def reader = http.request(Method.POST, ContentType.TEXT){
uri.path = "myPath.api"
send ContentType.XML, postBodyXml
}
def text = reader.getText()
def resultxml = new XmlSlurper().parseText(text.substring(1));