我可以模拟 NSHTTPURLResponse 的状态,但不能模拟它的 allHeaderFields
I can mock NSHTTPURLResponse's status but not its allHeaderFields
我正在使用 OCMock 来模拟 NSURLConnection/NSURLResponse(是的,我知道这些已被弃用)并且一切正常,因为我希望它模拟 http 响应代码和 http body。
现在我正在尝试扩展它并模拟 http header 字段,但我遇到编译错误并且不明白为什么。这是一些代码:
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];
我收到的错误是:使用未声明的标识符 "allHeaderFields"。
但是这里是 NSHTTPURLResponse 的定义,可以看到我正在模拟的状态和我收到错误的 allHeaderFields。由于定义了 allHeaderFields,我不明白为什么会出现此编译错误(如果我使用 OCMOCK_VALUE 而不是 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;
我通过这样做来让它工作:
OCMStub([gNSHTTPURLResponseMock allHeaderFields]).andReturn(header);
不知道为什么其他方法适用于 statusCode 但不适用于 allHeaderFields,但以上方法适用于 allHeaderFields。
我正在使用 OCMock 来模拟 NSURLConnection/NSURLResponse(是的,我知道这些已被弃用)并且一切正常,因为我希望它模拟 http 响应代码和 http body。 现在我正在尝试扩展它并模拟 http header 字段,但我遇到编译错误并且不明白为什么。这是一些代码:
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];
我收到的错误是:使用未声明的标识符 "allHeaderFields"。 但是这里是 NSHTTPURLResponse 的定义,可以看到我正在模拟的状态和我收到错误的 allHeaderFields。由于定义了 allHeaderFields,我不明白为什么会出现此编译错误(如果我使用 OCMOCK_VALUE 而不是 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;
我通过这样做来让它工作:
OCMStub([gNSHTTPURLResponseMock allHeaderFields]).andReturn(header);
不知道为什么其他方法适用于 statusCode 但不适用于 allHeaderFields,但以上方法适用于 allHeaderFields。