/core/externals/update-engine/externals/google-toolbox-for-mac/Foundation/GTMNSDictionary+URLArgumentsTest.m

http://macfuse.googlecode.com/ · Objective C · 87 lines · 63 code · 6 blank · 18 comment · 0 complexity · a62339a25f412ecb437d9d365d986257 MD5 · raw file

  1. //
  2. // GTMNSDictionary+URLArgumentsTest.m
  3. //
  4. // Copyright 2006-2008 Google Inc.
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. // use this file except in compliance with the License. You may obtain a copy
  8. // of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. // License for the specific language governing permissions and limitations under
  16. // the License.
  17. //
  18. #import "GTMSenTestCase.h"
  19. #import "GTMNSDictionary+URLArguments.h"
  20. #import "GTMDefines.h"
  21. @interface GTMNSDictionary_URLArgumentsTest : GTMTestCase
  22. @end
  23. @implementation GTMNSDictionary_URLArgumentsTest
  24. - (void)testFromArgumentsString {
  25. STAssertEqualObjects([NSDictionary gtm_dictionaryWithHttpArgumentsString:@""],
  26. [NSDictionary dictionary],
  27. @"- empty arguments string should give an empty dictionary");
  28. STAssertEqualObjects([NSDictionary gtm_dictionaryWithHttpArgumentsString:@"a"],
  29. [NSDictionary dictionaryWithObject:@"" forKey:@"a"],
  30. @"- missing '=' should result in an empty string value");
  31. STAssertEqualObjects([NSDictionary gtm_dictionaryWithHttpArgumentsString:@"a="],
  32. [NSDictionary dictionaryWithObject:@"" forKey:@"a"],
  33. @"- no value");
  34. STAssertEqualObjects([NSDictionary gtm_dictionaryWithHttpArgumentsString:@"&a=1"],
  35. [NSDictionary dictionaryWithObject:@"1" forKey:@"a"],
  36. @"- empty segment should be skipped");
  37. STAssertEqualObjects([NSDictionary gtm_dictionaryWithHttpArgumentsString:@"abc=123"],
  38. [NSDictionary dictionaryWithObject:@"123" forKey:@"abc"],
  39. @"- simple one-pair dictionary should work");
  40. STAssertEqualObjects([NSDictionary gtm_dictionaryWithHttpArgumentsString:@"a=1&a=2&a=3"],
  41. [NSDictionary dictionaryWithObject:@"1" forKey:@"a"],
  42. @"- only first occurrence of a key is returned");
  43. NSString* complex = @"a%2Bb=specialkey&complex=1%2B1%21%3D3%20%26%202%2A6%2F3%3D4&c";
  44. NSDictionary* result = [NSDictionary dictionaryWithObjectsAndKeys:
  45. @"1+1!=3 & 2*6/3=4", @"complex",
  46. @"specialkey", @"a+b",
  47. @"", @"c",
  48. nil];
  49. STAssertEqualObjects([NSDictionary gtm_dictionaryWithHttpArgumentsString:complex],
  50. result,
  51. @"- keys and values should be unescaped correctly");
  52. STAssertEqualObjects([NSDictionary gtm_dictionaryWithHttpArgumentsString:@"a=%FC"],
  53. [NSDictionary dictionaryWithObject:@"" forKey:@"a"],
  54. @"- invalid UTF8 characters result in an empty value, not a crash");
  55. }
  56. - (void)testArgumentsString {
  57. STAssertEqualObjects([[NSDictionary dictionary] gtm_httpArgumentsString], @"",
  58. @"- empty dictionary should give an empty string");
  59. STAssertEqualObjects([[NSDictionary dictionaryWithObject:@"123" forKey:@"abc"] gtm_httpArgumentsString],
  60. @"abc=123",
  61. @"- simple one-pair dictionary should work");
  62. NSDictionary* arguments = [NSDictionary dictionaryWithObjectsAndKeys:
  63. @"1+1!=3 & 2*6/3=4", @"complex",
  64. @"specialkey", @"a+b",
  65. nil];
  66. NSString* argumentString = [arguments gtm_httpArgumentsString];
  67. // check for individual pieces since order is not guaranteed
  68. NSString* component1 = @"a%2Bb=specialkey";
  69. NSString* component2 = @"complex=1%2B1%21%3D3%20%26%202%2A6%2F3%3D4";
  70. STAssertNotEquals([argumentString rangeOfString:component1].location, (NSUInteger)NSNotFound,
  71. @"- '%@' not found in '%@'", component1, argumentString);
  72. STAssertNotEquals([argumentString rangeOfString:component2].location, (NSUInteger)NSNotFound,
  73. @"- '%@' not found in '%@'", component2, argumentString);
  74. STAssertNotEquals([argumentString rangeOfString:@"&"].location, (NSUInteger)NSNotFound,
  75. @"- special characters should be escaped");
  76. STAssertNotEquals([argumentString characterAtIndex:0], (unichar)'&',
  77. @"- there should be no & at the beginning of the string");
  78. STAssertNotEquals([argumentString characterAtIndex:([argumentString length] - 1)], (unichar)'&',
  79. @"- there should be no & at the end of the string");
  80. }
  81. @end