Skip to content

Commit

Permalink
添加单元测试例子
Browse files Browse the repository at this point in the history
  • Loading branch information
剑子 authored and 剑子 committed Dec 20, 2019
1 parent 4732326 commit fc3f90c
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 2 deletions.
2 changes: 0 additions & 2 deletions AliyunOSSSDK/OSSNetworkingRequestDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ - (OSSTask *)buildInternalHttpRequest {
temComs.host = urlComponents.host;
temComs.port = urlComponents.port;



if ([self.allNeededMessage.bucketName oss_isNotEmpty]) {
OSSIPv6Adapter *ipAdapter = [OSSIPv6Adapter getInstance];
if ([OSSUtil isOssOriginBucketHost:temComs.host]) {
Expand Down
59 changes: 59 additions & 0 deletions AliyunOSSiOSTests/OSSUtilsTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,63 @@ - (void)testPerformanceExample {
}];
}

- (void)testBucketName{

///^[a-z0-9][a-z0-9\\-]{1,61}[a-z0-9]$"
BOOL result1 = [OSSUtil validateBucketName:@"123-456abc"];
XCTAssertTrue(result1);
BOOL result2 = [OSSUtil validateBucketName:@"123-456abc-"];
XCTAssertFalse(result2);
BOOL result3 = [OSSUtil validateBucketName:@"-123-456abc"];
XCTAssertFalse(result3);
BOOL result4 = [OSSUtil validateBucketName:@"123\\456abc"];
XCTAssertFalse(result4);

}


- (void)testEndpoint{
NSString *bucketName = @"test-image";
NSString *result1 = [self getResultEndpoint:@"http://123.test:8989/path?ooob" andBucketName:bucketName];
XCTAssertTrue([result1 isEqualToString:@"http://123.test:8989"]);


NSString *result2 = [self getResultEndpoint:@"http://192.168.0.1:8081" andBucketName:bucketName];
XCTAssertTrue([result2 isEqualToString:@"http://192.168.0.1:8081/test-image"]);

NSString *result3 = [self getResultEndpoint:@"http://192.168.0.1" andBucketName:bucketName];
XCTAssertTrue([result3 isEqualToString:@"http://192.168.0.1/test-image"]);

NSString *result4 = [self getResultEndpoint:@"http://oss-cn-region.aliyuncs.com" andBucketName:bucketName];
XCTAssertTrue([result4 isEqualToString:@"http://test-image.oss-cn-region.aliyuncs.com"]);

}

- (NSString *)getResultEndpoint:(NSString *)endpoint andBucketName:(NSString *)name{
NSURLComponents *urlComponents = [[NSURLComponents alloc]initWithString:endpoint];

NSURLComponents *temComs = [NSURLComponents new];
temComs.scheme = urlComponents.scheme;
temComs.host = urlComponents.host;
temComs.port = urlComponents.port;

if ([name oss_isNotEmpty]) {
OSSIPv6Adapter *ipAdapter = [OSSIPv6Adapter getInstance];
if ([OSSUtil isOssOriginBucketHost:temComs.host]) {
// eg. insert bucket to the begining of host.
temComs.host = [NSString stringWithFormat:@"%@.%@",
name, temComs.host];
if ([temComs.scheme.lowercaseString isEqualToString:@"http"] ) {
NSString *dnsResult = [OSSUtil getIpByHost: temComs.host];
temComs.host = dnsResult;
}
} else if ([ipAdapter isIPv4Address:temComs.host] || [ipAdapter isIPv6Address:temComs.host]) {
temComs.path = [NSString stringWithFormat:@"/%@",name];
}
}
return temComs.string;
}



@end
4 changes: 4 additions & 0 deletions OSSSwiftDemo/OSSSwiftDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
0F1C7D7023AC9425000B4E56 /* OSSUtilsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0F1C7D6F23AC9425000B4E56 /* OSSUtilsTests.swift */; };
D8350C7B1FFF66B400D8DF46 /* test.xml in Resources */ = {isa = PBXBuildFile; fileRef = D8350C6D1FFF66B400D8DF46 /* test.xml */; };
D8350C7C1FFF66B400D8DF46 /* wangwang.zip in Resources */ = {isa = PBXBuildFile; fileRef = D8350C6E1FFF66B400D8DF46 /* wangwang.zip */; };
D8350C801FFF66B400D8DF46 /* OSSSwiftGlobalDefines.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8350C761FFF66B400D8DF46 /* OSSSwiftGlobalDefines.swift */; };
Expand Down Expand Up @@ -67,6 +68,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
0F1C7D6F23AC9425000B4E56 /* OSSUtilsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSSUtilsTests.swift; sourceTree = "<group>"; };
D8350C6D1FFF66B400D8DF46 /* test.xml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = test.xml; sourceTree = "<group>"; };
D8350C6E1FFF66B400D8DF46 /* wangwang.zip */ = {isa = PBXFileReference; lastKnownFileType = archive.zip; path = wangwang.zip; sourceTree = "<group>"; };
D8350C701FFF66B400D8DF46 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -222,6 +224,7 @@
D8F27EAF200C4CD9005A2C39 /* OSSCnameTests.swift */,
D852773F2010B21A00D7B1DE /* SequentialMultipartUploadTests.swift */,
D83CEFF5201F1BFD00F917E1 /* TriggerCallBackTests.swift */,
0F1C7D6F23AC9425000B4E56 /* OSSUtilsTests.swift */,
);
path = OSSSwiftDemoTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -349,6 +352,7 @@
D8E64170200A31530023DF48 /* OSSProviderTests.swift in Sources */,
D8E641642009C0EB0023DF48 /* OSSExceptionalTests.swift in Sources */,
D8D4DE112008BCE9000B9EBB /* OSSCheckCRC64Tests.swift in Sources */,
0F1C7D7023AC9425000B4E56 /* OSSUtilsTests.swift in Sources */,
D83CEFF6201F1BFD00F917E1 /* TriggerCallBackTests.swift in Sources */,
D85277402010B21A00D7B1DE /* SequentialMultipartUploadTests.swift in Sources */,
D8E6416C2009D93F0023DF48 /* OSSLogTests.swift in Sources */,
Expand Down
74 changes: 74 additions & 0 deletions OSSSwiftDemo/OSSSwiftDemoTests/OSSUtilsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// OSSUtilsTests.swift
// OSSSwiftDemoTests
//
// Created by 剑子 on 2019/12/20.
// Copyright © 2019 aliyun. All rights reserved.
//

import XCTest

import AliyunOSSiOS
import AliyunOSSSwiftSDK


class OSSUtilsTests: XCTestCase {

override func setUp() {
// Put setup code here. This method is called before the invocation of each test method in the class.

// In UI tests it is usually best to stop immediately when a failure occurs.

// In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
}


func testBucketName(){
///^[a-z0-9][a-z0-9\\-]{1,61}[a-z0-9]$"
let result1 = OSSUtil.validateBucketName("123-456abc")
XCTAssertTrue(result1)
let result2 = OSSUtil.validateBucketName("123-456abc-")
XCTAssertFalse(result2)
let result3 = OSSUtil.validateBucketName("-123-456abc")
XCTAssertFalse(result3)
let str4 = String("123\\456abc")
let result4 = OSSUtil.validateBucketName(str4)
XCTAssertFalse(result4)
}

func testEndpoint(){
let bucketName = "test-image"
let result1 = getResultEndpoint(endpoint: "http://123.test:8989/path?ooob")
XCTAssertTrue((result1 == "http://123.test:8989"))
let result2 = getResultEndpoint(endpoint: "http://192.168.0.1:8081")
XCTAssertTrue((result2 == "http://192.168.0.1:8081/\(bucketName)"))
let result3 = getResultEndpoint(endpoint: "http://192.168.0.1")
XCTAssertTrue((result3 == "http://192.168.0.1/\(bucketName)"))
let result4 = getResultEndpoint(endpoint: "http://oss-cn-region.aliyuncs.com")
XCTAssertTrue((result4 == "http://\(bucketName).oss-cn-region.aliyuncs.com"))
}

func getResultEndpoint(endpoint : String) -> String {
let bucketName = "test-image"
let urlComs = URLComponents.init(string: endpoint)
var temComs = URLComponents.init()
temComs.scheme = urlComs?.scheme
temComs.host = urlComs?.host
temComs.port = urlComs?.port

if (bucketName as NSString).oss_isNotEmpty() {
let ipAdapter = OSSIPv6Adapter.getInstance()
if OSSUtil.isOssOriginBucketHost(temComs.host!) {
temComs.host = bucketName + "." + temComs.host!
if (temComs.scheme?.lowercased() == "http") {
let dnsResult = OSSUtil.getIpByHost(temComs.host!)
temComs.host = dnsResult
}
}else if(ipAdapter!.isIPv4Address(temComs.host!) || ipAdapter!.isIPv6Address(temComs.host!) ){
temComs.path = "/\(bucketName)"
}
}

return temComs.string!
}
}
1 change: 1 addition & 0 deletions Supporting Files/AliyunOSSiOS.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ FOUNDATION_EXPORT const unsigned char AliyunOSSiOSVersionString[];
#import "OSSService.h"
#import "OSSCompat.h"
#import "OSSXMLDictionary.h"
#import "OSSIPv6Adapter.h"

0 comments on commit fc3f90c

Please sign in to comment.