How to mock a POST request with mockk and fuel in kotlin? - http

I am making some http requests in kotlin with fuel library. I want to test that code using mockk library. I figured out how to mock http requests. Below is the code for that.
val client = mockk<Client>()
every { client.executeRequest(any()).statusCode } returns 200
every { client.executeRequest(any()).responseMessage } returns "test"
every { client.executeRequest(any()).data } returns "abc".toByteArray()
FuelManager.instance.client = client
assertEquals("abc" , testHttpRequest())
I do not like that any() here. I want to be specific about the http method and the url. I would like to return specific responses based on the url being called and the http method being used.
I figured may be I could do following
val req = Request(Method.POST, "my/test", URL("https://testRequest.com"), timeoutInMillisecond = 3000, timeoutReadInMillisecond = 3000)
every { client.executeRequest(req).statusCode } returns 200
every { client.executeRequest(req).responseMessage } returns "OK"
every { client.executeRequest(req).data } returns "abc".toByteArray()
FuelManager.instance.client = client
But I am getting following error.
io.mockk.MockKException: no answer found for: Client(#1).executeRequest(-->
https://testRequest.com/my/test
"Body : abc"
"Headers : (3)"
Accept-Encoding : compress;q=0.5, gzip;q=1.0
Content-Type : application/json
Authorization : Basic xxx)
What am I missing here?

To all those people that ended up here trying to find a solution to this, I've found something that solves the problem for my use case (but there are likely many use cases it's not appropriate for and I accept that it may not be the nicest...).
Provided you always have the calls to different endpoints in the same order every time you can do -
every { client.executeRequest(any()).data} returnsMany listOf(responseBody1, responseBody2, ... , responseBodyN)
Which will return the next response body for every subsequent call to the Fuel client.
The full code would look like (using OP's example) -
val response1 = "This is response one"
val response2 = "This is response two"
val client = mockk<Client>()
every { client.executeRequest(any()).statusCode } returns 200
every { client.executeRequest(any()).responseMessage } returns "test"
every { client.executeRequest(any()).data } returnsMany listOf(response1.toByteArray(), response2.toByteArray())
FuelManager.instance.client = client
assertEquals("This is response one", testHttpRequest())
assertEquals("This is response two", testHttpRequest())
I suspect the correct way to do this is with a 'CustomMatcher' extension function on the MockKMatcherScope as detailed here. I could only get the mock to response with the last item that'd been mocked when doing that, rather than the correct item but YMMV...

Try to use following:
every { client.executeRequest(req) } returns <mock object>

You could try these lines. Regarding how to intercept fuel request.
fun interceptFuel(method: Method, url: String) {
val interceptor = { next: (Request) -> Request ->
{ req: Request ->
if (req.method == method && req.url.toString() == url) {
val client = mockk<Client>()
/*mock fuel into whatever you like*/
FuelManager.instance.client = client
}
next(req)
}
}
FuelManager.instance.addRequestInterceptor(interceptor)
}
then, use it like this
interceptFuel(Method.GET, "https://google.com")
BTW, this code not fully tested. Use at your own risk

Related

How do I make this example of an HTTP request work?

I am trying to make the example in the documentation work, but I can't.
use http::{Request, Response};
let mut request = Request::builder();
request.uri("https://www.rust-lang.org/")
.header("User-Agent", "my-awesome-agent/1.0");
if needs_awesome_header() {
request.header("Awesome", "yes");
}
let response = send(request.body(()).unwrap());
fn send(req: Request<()>) -> Response<()> {
// ...
}
The question is, how can I print the response in a string to save it? It seems it is not in the response.

POST data empty ( or not exist ) when I receive post back from TPV provider

I'm trying to implement a service Redsys payments on my .net website.
The payment is successful (data are sent by post) but when the POST data back to my website ( to confirm the payment ) and i try to retrieve them with:
Request.form string value = [ "name"]
the value is always empty
I tried to count how many are in Request.Form.Keys.Count and always gives me zero values.
In the vendor documentation it indicated that the variables may be collected with Request.form [ "name"] and I called them to ask why I do not get the data and they dont know why...so I'm desperate,
What may be due?
I have reviewed the header I get from the server ( width Request.Headers ) and have the following parameters. HttpMethod:: GET Requestype: GET and contentlength: 0 . My bank tell me that they response POST not GET... so it´s a mistery. May be the error is becouse that sendings are made from a https to a htttp ?
You are receiving a POST without parameters.
The parameters are in the content of the call.
You should read the content and get the values of each parameter:
[System.Web.Http.HttpPost]
public async Task<IHttpActionResult> PostNotification()
{
string body = "";
await
Request.Content.ReadAsStreamAsync().ContinueWith(x =>
{
var result = "";
using (var sr = new StreamReader(x.Result))
{
result = sr.ReadToEnd();
}
body += result;
});
In body you can read the parameters (the order of them can change).

In node.js, how do I get Request Unit Charges from call to DocumentClient.queryDocuments()?

I'm using the queryDocuments() method from the node.js client for the first time. I've previously used readDocument(), executeStoredProcedure(), replaceStoredProcedure(), etc. and all of those return a header object that allows you to inspect the 'x-ms-request-charge' header to discern the request unit charge for the operation. However, when using the queryDocuments() or readDocuments() methods, it returns an QueryIterator and I don't see a way to inspect it to see the RUs for the operation.
I suspect that the reason for this is that it's not one operation. It's a series of operations and you didn't implement a way to aggregate the total RUs for the entire thing. I'm willing to hit the REST API directly to get this myself, but I wanted to ask if there was a way to get it with the current library before I went through the trouble.
You can inspect response headers (e.g. x-ms-request-charge) by inspecting the third input parameter in the callback for queryIterator.executeNext().
For example, here is a code sample that uses the response header (x-ms-retry-after-ms) to implement retry logic on throttles (error 429).
var queryIterator = documentClient.queryDocuments(collection._self, query);
executeNextWithRetry(yourCallback);
function executeNextWithRetry(callback) {
queryIterator.executeNext(function(err, results, responseHeaders) {
if(err && err.code === 429 && responseHeaders['x-ms-retry-after-ms']) {
console.log("Retrying after " + responseHeaders['x-ms-retry-after-ms']);
setTimeout(function() {
executeNextWithRetry(callback);
}, responseHeaders['x-ms-retry-after-ms']);
} else {
callback(err, results, responseHeaders);
}
});
}

Get HTTP Status using swift

I am sorry, I haven't found an answer for my question(( Please, don't be very harsh, I am not a professional programmer, but I keep learning and hope once I will be able to answer someone's question))
I am trying to get HTTP Status of the link (I am sort of generating links depending on one database entries code, like ABCDEF, I keep them in an array and then generate a link to second database, like www.blablabla.ABCDEF.net), so I can see whether the page exists in database or not.
I have written this code, but something is wrong. So maybe it's a question like: "What's wrong with my code?" But on the stack they also say, you have to show your attempts of problem solving...
I wish I could keep it all swift, without any additional modules or something, I think NSHTTPURLResponse must be enough, but I am using it somehow wrong.
Looking forward to help and replies))
var err: NSError!
NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{ (response: NSURLResponse?, data: NSData!, error: err) -> Void in
if (err != nil) {
let httpStatus: NSHTTPURLResponse = response as NSHHTPURLResponse
for myLink in allLinks {
println("HERE IS THE CURRENT STATUS CODE" + httpStatus.statusCode + "OF A LINK:" + myLink)
if httpStatus.statusCode == 200 {println("SUCCESS!!!!!")}
}
}
}
The fundamental issue here is that you appear to be looking at the statusCode only if the err is not nil. But if you have error, you probably don't have status code. The error parameter to the closure indicates fundamental network issue that probably prevented you from getting to the page in question. The statusCode is generally only meaningful if the error was nil (i.e. you succeeded in connecting to the server), in which case the statusCode is how the server informs you of its ability to service the HTTP request.
A couple of minor things:
You don't need the var err: NSError! line (because the error object is passed as parameter to the closure). This variable you've declared is not used here.
I don't see how error: err as the third parameter to the closure could have worked. The syntax is "variableName: variableType", but there is no type of err.
Likewise, your code is referring to a non-existent class, NSHHTPURLResponse. In Swift 3 and later, it's HTTPURLResponse.
It's probably prudent to do if let for the retrieval of the HTTPURLResponse in order to get the statusCode.
I'm unclear as to your intent in iterating through allLinks, because this connection is just for a given request, not a bunch of links. Just look at the statusCode in light of the particular request. If you need to test multiple URLs, then you do a separate request for each.
We should consider any codes between 200 and 299 as success, not just 200. I'd suggest using the range 200 ..< 300.
Thus:
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, let httpResponse = response as? HTTPURLResponse, error == nil else {
print("No valid response")
return
}
guard 200 ..< 300 ~= httpResponse.statusCode else {
print("Status code was \(httpResponse.statusCode), but expected 2xx")
return
}
// everything OK, process `data` here
}
task.resume()
I also made a few other changes (updated for Swift 3 and later; use URLSession rather than URLConnection; I think error is fine choice for the variable name; I prefer the trailing closure syntax; I tend to use inferred types for closure parameters to make the declaration a little more concise, etc.), but all of that is immaterial to the question at hand: Hopefully this illustrates how one checks the status code.
For Swift 2 rendition, see previous revision of this answer.
[Swift 5.2]
Hi there, you can try this one:
let task = URLSession.shared.dataTask(with: yourRequest) {
(data, response, error) in
guard let response = response else {
print("Cannot found the response")
return
}
let myResponse = response as! HTTPURLResponse
print("Status Code:", myResponse.statusCode)
}
task.resume()
Here is the output
Status Code: 200
(This is your StatusCode)

Accessing spray request from spray response

I'm calling this function every 50 ms :
def send() = {
val myData = generateRandomData()
val response = pipeline(Post("http://slow-website.com/send", myData))
response onComplete {
case Success(r) => ? how to access myData ?
case Failure(error) => print(error.getMessage)
}
}
I would like to know what data was sent in my successfull request.
How can I achieve this?
Just refer to myData.
What happens behind the scenes is that the Scala compiler creates a closure for the onComplete handler argument that captures the reference to myData so that you can use it.

Resources