2016-01-04 18:39:07 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
2016-01-04 18:39:07 +00:00
|
|
|
*
|
2017-05-06 03:50:47 +00:00
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2016-01-04 18:39:07 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <XCTest/XCTest.h>
|
2016-11-24 17:44:51 +00:00
|
|
|
|
|
|
|
#import <React/RCTUtils.h>
|
2016-01-04 18:39:07 +00:00
|
|
|
|
|
|
|
@interface RCTURLUtilsTests : XCTestCase
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTURLUtilsTests
|
|
|
|
|
|
|
|
- (void)testGetQueryParam
|
|
|
|
{
|
|
|
|
NSURL *URL = [NSURL URLWithString:@"http://example.com?foo=bar&bar=foo"];
|
|
|
|
NSString *foo = RCTGetURLQueryParam(URL, @"foo");
|
|
|
|
NSString *bar = RCTGetURLQueryParam(URL, @"bar");
|
|
|
|
XCTAssertEqualObjects(foo, @"bar");
|
|
|
|
XCTAssertEqualObjects(bar, @"foo");
|
|
|
|
}
|
|
|
|
|
2016-01-05 20:50:25 +00:00
|
|
|
- (void)testGetEncodedParam
|
|
|
|
{
|
|
|
|
NSURL *URL = [NSURL URLWithString:@"http://example.com?foo=You%20%26%20Me"];
|
|
|
|
NSString *foo = RCTGetURLQueryParam(URL, @"foo");
|
|
|
|
XCTAssertEqualObjects(foo, @"You & Me");
|
|
|
|
}
|
|
|
|
|
2016-01-04 18:39:07 +00:00
|
|
|
- (void)testQueryParamNotFound
|
|
|
|
{
|
|
|
|
NSURL *URL = [NSURL URLWithString:@"http://example.com?foo=bar"];
|
|
|
|
NSString *bar = RCTGetURLQueryParam(URL, @"bar");
|
|
|
|
XCTAssertNil(bar);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testDuplicateParamTakesLatter
|
|
|
|
{
|
|
|
|
NSURL *URL = [NSURL URLWithString:@"http://example.com?foo=bar&foo=foo"];
|
|
|
|
NSString *foo = RCTGetURLQueryParam(URL, @"foo");
|
|
|
|
XCTAssertEqualObjects(foo, @"foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testNilURLGetQueryParam
|
|
|
|
{
|
|
|
|
NSURL *URL = nil;
|
|
|
|
NSString *foo = RCTGetURLQueryParam(URL, @"foo");
|
|
|
|
XCTAssertNil(foo);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testReplaceParam
|
|
|
|
{
|
|
|
|
NSURL *URL = [NSURL URLWithString:@"http://example.com?foo=bar&bar=foo"];
|
|
|
|
NSURL *result = RCTURLByReplacingQueryParam(URL, @"foo", @"foo");
|
|
|
|
XCTAssertEqualObjects(result.absoluteString, @"http://example.com?foo=foo&bar=foo");
|
|
|
|
}
|
|
|
|
|
2016-01-05 20:50:25 +00:00
|
|
|
- (void)testReplaceEncodedParam
|
|
|
|
{
|
|
|
|
NSURL *URL = [NSURL URLWithString:@"http://example.com?foo=You%20%26%20Me"];
|
|
|
|
NSURL *result = RCTURLByReplacingQueryParam(URL, @"foo", @"Me & You");
|
|
|
|
XCTAssertEqualObjects(result.absoluteString, @"http://example.com?foo=Me%20%26%20You");
|
|
|
|
}
|
|
|
|
|
2016-01-04 18:39:07 +00:00
|
|
|
- (void)testAppendParam
|
|
|
|
{
|
|
|
|
NSURL *URL = [NSURL URLWithString:@"http://example.com?bar=foo"];
|
|
|
|
NSURL *result = RCTURLByReplacingQueryParam(URL, @"foo", @"bar");
|
|
|
|
XCTAssertEqualObjects(result.absoluteString, @"http://example.com?bar=foo&foo=bar");
|
|
|
|
}
|
|
|
|
|
2016-01-06 19:54:25 +00:00
|
|
|
- (void)testRemoveParam
|
|
|
|
{
|
|
|
|
NSURL *URL = [NSURL URLWithString:@"http://example.com?bar=foo&foo=bar"];
|
|
|
|
NSURL *result = RCTURLByReplacingQueryParam(URL, @"bar", nil);
|
|
|
|
XCTAssertEqualObjects(result.absoluteString, @"http://example.com?foo=bar");
|
|
|
|
}
|
|
|
|
|
2016-01-04 18:39:07 +00:00
|
|
|
- (void)testNilURLAppendQueryParam
|
|
|
|
{
|
|
|
|
NSURL *URL = nil;
|
|
|
|
NSURL *result = RCTURLByReplacingQueryParam(URL, @"foo", @"bar");
|
|
|
|
XCTAssertNil(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|