iosobjective-cocmock

I can mock NSHTTPURLResponse's status but not its allHeaderFields


I'm using OCMock to mock NSURLConnection/NSURLResponse (yes I do know these are deprecated) and everything is working as I want it mocking the http response code and the http body. Now I'm trying to extend it and also mock the http header fields, but I'm getting a compilation error and don't understand why. Here's some code:

id   gNSHTTPURLResponseMock = [OCMockObject niceMockForClass:[NSHTTPURLResponse class]];

...

    [[[gNSHTTPURLResponseMock stub] andReturnValue:OCMOCK_VALUE(statusCode)] statusCode];
     if (header)
     {
         [[[gNSHTTPURLResponseMock stub] andReturnValue:OCMOCK_ANY(allHeaderFields)] header];
     }
     [gNidHTTPConnectionObject connection:gDummyUrlConnection didReceiveResponse:gNSHTTPURLResponseMock];

The error I'm getting is it says: use of undeclared identifier "allHeaderFields". But here's the definition of NSHTTPURLResponse, there can be seen status which I'm mocking and allHeaderFields for which I'm getting the error. As allHeaderFields is defined I can't understand why I get this compilation error (its the same error if I use OCMOCK_VALUE instead of OCMOCK_ANY)

@interface NSHTTPURLResponse : NSURLResponse 
{
    @package
    NSHTTPURLResponseInternal *_httpInternal;
}

/*!
  @method   initWithURL:statusCode:HTTPVersion:headerFields:
  @abstract initializer for NSHTTPURLResponse objects.
  @param    url the URL from which the response was generated.
  @param    statusCode an HTTP status code.
  @param    HTTPVersion The version of the HTTP response as represented by the server.  This is typically represented as "HTTP/1.1".
  @param    headerFields A dictionary representing the header keys and values of the server response.
  @result   the instance of the object, or NULL if an error occurred during initialization.
  @discussion This API was introduced in Mac OS X 10.7.2 and iOS 5.0 and is not available prior to those releases.
*/
- (nullable instancetype)initWithURL:(NSURL *)url statusCode:(NSInteger)statusCode HTTPVersion:(nullable NSString *)HTTPVersion headerFields:(nullable NSDictionary<NSString *, NSString *> *)headerFields API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));

/*! 
    @abstract Returns the HTTP status code of the receiver. 
    @result The HTTP status code of the receiver. 
*/
@property (readonly) NSInteger statusCode;

/*! 
    @abstract Returns a dictionary containing all the HTTP header fields
    of the receiver.
    @discussion By examining this header dictionary, clients can see
    the "raw" header information which was reported to the protocol
    implementation by the HTTP server. This may be of use to
    sophisticated or special-purpose HTTP clients.
    @result A dictionary containing all the HTTP header fields of the
    receiver.
*/
@property (readonly, copy) NSDictionary *allHeaderFields;

Solution

  • I got it to work by doing this instead:

     OCMStub([gNSHTTPURLResponseMock allHeaderFields]).andReturn(header);
    

    No idea why the other way works for statusCode but not for allHeaderFields, but the above works for allHeaderFields.