Coder Social home page Coder Social logo

jangorman / hippolyte Goto Github PK

View Code? Open in Web Editor NEW
111.0 7.0 18.0 152 KB

HTTP Stubbing in Swift

License: MIT License

Ruby 3.21% Swift 95.50% Objective-C 1.29%
swift stubbing testing xctest cocoapods mock mocking test ios stub http-stub swift-5 swift5 macos carthage spm swift-package-manager

hippolyte's People

Contributors

clemens-schulz avatar davbeck avatar davidsansome avatar dependabot-preview[bot] avatar doronkatz avatar fumoboy007 avatar furrki avatar garfieldonthemoon avatar iamtomcat avatar jangorman avatar joegrist avatar onevcat avatar richardpineo avatar srekke avatar tjnet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

hippolyte's Issues

Logo

Hi, concrats for your project!
Do you have a logotype? If you need one I can do it for free, I love to contribute to opensource projects, so if you want one just say it, Im here to help!

Greetings!
Nuno Jesus

Support for XCUITest ?

This is quick question not an issue.

DO you think this approach will wok to stub the Xcode UI Tests as well ?

@JanGorman Let me know what you think ?

Hippolyte.shared.stop() not working as expected

Hello. We are using version 1.2.3 of Hippolyte with Xcode 12.1, macOS Catalina 10.15.7. We are seeing that Hippolyte.shared.stop() is not working the way we expect.

We have a test suite that includes tests where we do not want to mock HTTP transactions. Other tests in our test suite use Hippolyte where we do want mocking. Our tearDown() function includes the call Hippolyte.shared.stop(), but we are seeing tests where we do not want mocking fail with

Error Domain=Hippolyte.HTTPStubURLProtocol.NoMatchError Code=1 "(null)" UserInfo={_NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <A5D03C08-843F-451B-9871-633EED5F4DC9>.<35>"
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <A5D03C08-843F-451B-9871-633EED5F4DC9>.<35>}

In the test suite, there are tests that use Hippolyte that execute before the failing test.
If we run the failing test by itself, it passes.

We expected Hippolyte.shared.stop() to disable Hippolyte and allow unmocked HTTP transactions. Is that the correct expectation?

Undefined symbols for architecture x86_64

Hello, I'm getting 'Undefined symbols for architecture x86_64' after adding this framework. I've cleaned my derived data folder, made sure linker flags have '$(inherited) and made sure build archive arch was set to NO and I can't get this framework to build. Any ideas?

304 No content not handled correctly

I was using Hippolyte to stub out my tests and found that the 304 response is not handled correctly.

https://github.com/JanGorman/Hippolyte/blob/master/Hippolyte/HTTPStubURLProtocol.swift#L46

// Original, incorrect:
      if 300...399 ~= statusCode && (statusCode != 304 || statusCode != 305) {

// Fixed:
      if 300...399 ~= statusCode && (statusCode != 304 && statusCode != 305) {

I'm not actually sure why 305 is handled at all, since I think that status code is not suggested, but 304 is a valid code.

Here is a test that can be added to the Hippolyte tests that demonstrates the failure:

  func testItDoesNotStubsRedirectNoContent() {
    let url = URL(string: "http://www.apple.com")!
    var stub = StubRequest(method: .GET, url: url)
    var response = StubResponse()
    response = StubResponse(statusCode: 304)
    stub.response = response
    Hippolyte.shared.add(stubbedRequest: stub)

    Hippolyte.shared.start()

    let expectation = self.expectation(description: "Dies not stub network redirect call")
    let delegate = BlockRedirectDelegate()
    let session = URLSession(configuration: .default, delegate: delegate, delegateQueue: nil)
    let task = session.dataTask(with: url) { data, _, _ in
      expectation.fulfill()
    }
    task.resume()
    wait(for: [expectation], timeout: 1)

    XCTAssertEqual(delegate.redirectCallCount, 0)
  }

Should I have forked and tried to submit a branch with the fix?

Testing POST api with this

Hi,

I want to test the post Api call. But I am not getting how to send the parameters to request.
The sample code give is for .GET var stub = StubRequest(method: .GET, url: gitUrl)

Please help me out with .POST var stub = StubRequest(method: .POST, url: gitUrl)

stub request not found

I am keen to unit test, getting stub request not found while I have set it up rightly and passing a valid json data as a response.
pasting the error log, please guide me the correct way. Also I am making a request with POST method, but getting the request as GET in the error.

Printing description of error:
▿ Optional
▿ some : No matching stub found for https://testdemo/v0.4/login/
▿ request : https://testdemo/v0.4/login/
▿ url : Optional
▿ some : https://testdemo/v0.4/login/
- cachePolicy : 0
- timeoutInterval : 60.0
- mainDocumentURL : nil
- networkServiceType : __ObjC.NSURLRequest.NetworkServiceType
- allowsCellularAccess : true
▿ httpMethod : Optional
- some : "GET"
▿ allHTTPHeaderFields : Optional<Dictionary<String, String>>
- some : 0 elements
- httpBody : nil
- httpBodyStream : nil
- httpShouldHandleCookies : true
- httpShouldUsePipelining : false
(lldb)

Carthage compatible

Just a question

Is it Carthage compatible?
If not, any plans for the compatibility in the nearest future?

HTTPStubURLProtocol.NoMatchError

Hello,

I am trying to test a POST request by sending CredentialParams struct containing the username & password.
As a response, I do expect valid access & refresh token, but when I try to run it up, it shows some weird error, which does not tell me a lot.

2022-07-01 13:29:41.737535+0300 Kollektiv Creators Staging[33888:4167655] Task .<1> finished with error [1] Error Domain=Hippolyte.HTTPStubURLProtocol.NoMatchError Code=1 "(null)" UserInfo={_NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask .<1>"
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask .<1>}

My own code:

`let body = CredentialsParams(email: "[email protected]",
password: "nEjh35921")
let matcher = JSONMatcher(object: body)

    let url = URL(string: "http://our-api-domain/api/v1/user/login")!
    
    let response = StubResponse.Builder()
        .stubResponse(withStatusCode: 204)
        .build()
    
    let request = StubRequest.Builder()
        .stubRequest(withMethod: .POST, url: url)
        .addMatcher(matcher)
        .addResponse(response)
        .build()
    
    Hippolyte.shared.add(stubbedRequest: request)
    Hippolyte.shared.start()
    
    let expectation = self.expectation(description: "Stubs network call")
    let task = URLSession.shared.dataTask(with: url) { data, _, _ in
        
        print("Data \(data)") - Data is always nil
        expectation.fulfill()
    }
    task.resume()
    
    wait(for: [expectation], timeout: 1)`

Handle 304 HTTP Status Code

Hi,

I'm trying to test a response using this http status code but it's not working. I don't know if it's possible to test that response withStatusCode = 304 or there are any errors on my code.

Many thanks.

Possible to wait to respond to request?

Hippolyte has worked great for testing a lot of different scenarios in our app, but there's one thing I haven't figured out how to test for, and that's behavior while a network request is in flight. For instance, I have a class that returns in flight tasks if the same file is requested again. Another class needs to handle cancellation while a request is in progress.

For other async operations that I've stubbed I'd do the following:

  • stub request in a pending state
  • perform the operation that uses the request
  • make checks or actions based on a pending request state
  • resume request stub

Hippolyte has a pause method, but from what I understand that just stops it from responding to requests temporarily.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.