Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use this snippet to stub the request #144

Open
ivangodfather opened this issue Jul 13, 2016 · 1 comment
Open

Use this snippet to stub the request #144

ivangodfather opened this issue Jul 13, 2016 · 1 comment

Comments

@ivangodfather
Copy link

Im trying to stub a request with a specific body, i'm copying it from Charles but im getting always the same error:

Use this snippet to stub the request:
stubRequest(@"PUT", @"http://url.com").
withHeaders(@{ @"Accept": @"application/json", @"Accept-Encoding": @"gzip;q=1.0, compress;q=0.5", @"Accept-Language": @"en-US;q=1.0", @"Content-Length": @"108", @"Content-Type": @"application/json", @"User-Agent": @"Unknown/Unknown (Unknown; OS Version 9.3 (Build 13E230))", @"X-deviceToken": @"deviceToken", @"X-platformId": @"3", @"deviceId": @"deviceId", @"languageId": @"3" }).
withBody(@"{
  \"matchIds\" : [
    \"17\",
    \"MatchId_3\",
    \"MatchId_34\"
  ],
  \"values\" : {
    \"viewed\" : true
  }
}");

And this in my test:

    let urlPut = "http://url.com"
    stubRequest("PUT", urlPut)
        .withBody(fromJsonFile("getMatchesRequestPUTBody"))
        .andReturn(200)
        .withHeaders(["Content-Type": "application/json"])

Where getMatchesRequestPUTBody contains:
{
"matchIds" : [
"17",
"MatchId_3",
"MatchId_34"
],
"values" : {
"viewed" : true
}
}
If i remove the line "withBody" it works as expected.
Any ideas?
Notice i'm stubbing 2 request in the same method.

    public func fromJsonFile(fileName: String) -> String {
        return fromFile(fileName, fileExtension: "json")
    }

    public func fromTxtFile(fileName: String) -> String {
        return fromFile(fileName, fileExtension: "txt")
    }

    public func fromFile(fileName: String, fileExtension: String) -> String {
        let classBundle = NSBundle(forClass: self.classForCoder)
        let path = classBundle.pathForResource(fileName, ofType: fileExtension)
        let absolutePath =  path ?? ""
        do {
            return try String(contentsOfFile: absolutePath, encoding: NSUTF8StringEncoding)
        } catch _ {
            print("Error trying to read file \(absolutePath). The file does not exist")
            return ""
        }
    }
@andrewkboyd
Copy link
Collaborator

What is the error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants