FBRouteTests.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. */
  9. #import <XCTest/XCTest.h>
  10. #import "FBRoute.h"
  11. @class RouteResponse;
  12. @interface FBHandlerMock : NSObject
  13. @property (nonatomic, assign) BOOL didCallSomeSelector;
  14. @end
  15. @implementation FBHandlerMock
  16. - (id)someSelector:(id)arg
  17. {
  18. self.didCallSomeSelector = YES;
  19. return nil;
  20. };
  21. @end
  22. @interface FBRouteTests : XCTestCase
  23. @end
  24. @implementation FBRouteTests
  25. - (void)testGetRoute
  26. {
  27. FBRoute *route = [FBRoute GET:@"/"];
  28. XCTAssertEqualObjects(route.verb, @"GET");
  29. }
  30. - (void)testPostRoute
  31. {
  32. FBRoute *route = [FBRoute POST:@"/"];
  33. XCTAssertEqualObjects(route.verb, @"POST");
  34. }
  35. - (void)testPutRoute
  36. {
  37. FBRoute *route = [FBRoute PUT:@"/"];
  38. XCTAssertEqualObjects(route.verb, @"PUT");
  39. }
  40. - (void)testDeleteRoute
  41. {
  42. FBRoute *route = [FBRoute DELETE:@"/"];
  43. XCTAssertEqualObjects(route.verb, @"DELETE");
  44. }
  45. - (void)testTargetAction
  46. {
  47. FBHandlerMock *mock = [FBHandlerMock new];
  48. FBRoute *route = [[FBRoute new] respondWithTarget:mock action:@selector(someSelector:)];
  49. [route mountRequest:(id)NSObject.new intoResponse:(id)NSObject.new];
  50. XCTAssertTrue(mock.didCallSomeSelector);
  51. }
  52. - (void)testRespond
  53. {
  54. XCTestExpectation *expectation = [self expectationWithDescription:@"Calling respond block works!"];
  55. FBRoute *route = [[FBRoute new] respondWithBlock:^id<FBResponsePayload>(FBRouteRequest *request) {
  56. [expectation fulfill];
  57. return nil;
  58. }];
  59. [route mountRequest:(id)NSObject.new intoResponse:(id)NSObject.new];
  60. [self waitForExpectationsWithTimeout:0.0 handler:nil];
  61. }
  62. - (void)testRouteWithSessionWithSlash
  63. {
  64. FBRoute *route = [[FBRoute POST:@"/deactivateApp"] respondWithTarget:self action:@selector(dummyHandler:)];
  65. XCTAssertEqualObjects(route.path, @"/session/:sessionID/deactivateApp");
  66. }
  67. - (void)testRouteWithSession
  68. {
  69. FBRoute *route = [[FBRoute POST:@"deactivateApp"] respondWithTarget:self action:@selector(dummyHandler:)];
  70. XCTAssertEqualObjects(route.path, @"/session/:sessionID/deactivateApp");
  71. }
  72. - (void)testRouteWithoutSessionWithSlash
  73. {
  74. FBRoute *route = [[FBRoute POST:@"/deactivateApp"].withoutSession respondWithTarget:self action:@selector(dummyHandler:)];
  75. XCTAssertEqualObjects(route.path, @"/deactivateApp");
  76. }
  77. - (void)testRouteWithoutSession
  78. {
  79. FBRoute *route = [[FBRoute POST:@"deactivateApp"].withoutSession respondWithTarget:self action:@selector(dummyHandler:)];
  80. XCTAssertEqualObjects(route.path, @"/deactivateApp");
  81. }
  82. - (void)testEmptyRouteWithSession
  83. {
  84. FBRoute *route = [[FBRoute POST:@""] respondWithTarget:self action:@selector(dummyHandler:)];
  85. XCTAssertEqualObjects(route.path, @"/session/:sessionID");
  86. }
  87. - (void)testEmptyRouteWithoutSession
  88. {
  89. FBRoute *route = [[FBRoute POST:@""].withoutSession respondWithTarget:self action:@selector(dummyHandler:)];
  90. XCTAssertEqualObjects(route.path, @"/");
  91. }
  92. - (void)testEmptyRouteWithSessionWithSlash
  93. {
  94. FBRoute *route = [[FBRoute POST:@"/"] respondWithTarget:self action:@selector(dummyHandler:)];
  95. XCTAssertEqualObjects(route.path, @"/session/:sessionID");
  96. }
  97. - (void)testEmptyRouteWithoutSessionWithSlash
  98. {
  99. FBRoute *route = [[FBRoute POST:@"/"].withoutSession respondWithTarget:self action:@selector(dummyHandler:)];
  100. XCTAssertEqualObjects(route.path, @"/");
  101. }
  102. + (id<FBResponsePayload>)dummyHandler:(FBRouteRequest *)request
  103. {
  104. return nil;
  105. }
  106. @end