luisobo / Nocilla

Testing HTTP requests has never been easier. Nocilla: Stunning HTTP stubbing for iOS and Mac OS X.
https://twitter.com/luisobo
MIT License
1.84k stars 172 forks source link

Use this snippet to stub the request #144

Open ivangodfather opened 8 years ago

ivangodfather commented 8 years ago

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 commented 8 years ago

What is the error?