-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. new optional parameter "enableRedirect" for hs.http.doAsyncRequest 2. add 4 tests to test this change pr-feature
- Loading branch information
Showing
4 changed files
with
224 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// HShttp.m | ||
// Hammerspoon | ||
// | ||
// Created by Alex Chen on 08/21/2022. | ||
|
||
#import "HSTestCase.h" | ||
|
||
@interface HThttp : HSTestCase | ||
|
||
@end | ||
|
||
@implementation HThttp | ||
|
||
- (void)setUp { | ||
[super setUpWithRequire:@"test_http"]; | ||
// Put setup code here. This method is called before the invocation of each test method in the class. | ||
} | ||
|
||
- (void)tearDown { | ||
// Put teardown code here. This method is called after the invocation of each test method in the class. | ||
[super tearDown]; | ||
} | ||
|
||
// Http tests | ||
|
||
- (void)testHttpDoAsyncRequestWithCachePolicyParam { | ||
RUN_TWO_PART_LUA_TEST_WITH_TIMEOUT(5) | ||
} | ||
|
||
- (void)testHttpDoAsyncRequestWithRedirectParamButNoCachePolicyParam { | ||
RUN_LUA_TEST() | ||
} | ||
|
||
- (void)testHttpDoAsyncRequestWithNoEnableRedirectParam { | ||
RUN_TWO_PART_LUA_TEST_WITH_TIMEOUT(5) | ||
} | ||
|
||
- (void)testHttpDoAsyncRequestWithRedirection { | ||
RUN_TWO_PART_LUA_TEST_WITH_TIMEOUT(5) | ||
} | ||
|
||
- (void)testHttpDoAsyncRequestWithoutRedirection { | ||
RUN_TWO_PART_LUA_TEST_WITH_TIMEOUT(5) | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
-- hs.http = require("hs.http") | ||
-- hs = require("hs") | ||
|
||
_G["respCode"] = 0 | ||
_G["respBody"] = "" | ||
_G["respHeaders"] = {} | ||
|
||
_G["callback"] = function(code, body, headers) | ||
_G["respCode"] = code | ||
_G["respBody"] = body | ||
_G["respHeaders"] = headers | ||
end | ||
|
||
function testHttpDoAsyncRequestWithCachePolicyParamValues() | ||
if (type(_G["respCode"]) == "number" and type(_G["respBody"]) == "string" and type(_G["respHeaders"]) == "table") then | ||
-- check return code | ||
assertIsEqual(200, _G["respCode"]) | ||
assertGreaterThan(0, string.len(_G["respBody"])) | ||
return success() | ||
else | ||
return "Waiting for success..." | ||
end | ||
end | ||
|
||
-- check request should be redirected if [enableRedirect|cachePolicy] param is given as cachePolicy | ||
-- check point: response code == 200 | ||
function testHttpDoAsyncRequestWithCachePolicyParam() | ||
_G["respCode"] = 0 | ||
_G["respBody"] = "" | ||
_G["respHeaders"] = {} | ||
hs.http.doAsyncRequest( | ||
'http://google.com', | ||
'GET', | ||
nil, | ||
{ ['accept-language'] = 'en', ['user-agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36', Accept = '*/*' }, | ||
_G["callback"], | ||
'protocolCachePolicy' | ||
) | ||
|
||
return success() | ||
end | ||
|
||
function testHttpDoAsyncRequestWithoutEnableRedirectAndCachePolicyParamValues() | ||
if (type(_G["respCode"]) == "number" and type(_G["respBody"]) == "string" and type(_G["respHeaders"]) == "table") then | ||
-- check return code | ||
assertIsEqual(200, _G["respCode"]) | ||
assertGreaterThan(0, string.len(_G["respBody"])) | ||
return success() | ||
else | ||
return "Waiting for success..." | ||
end | ||
end | ||
|
||
-- check request should be redirected if [enableRedirect|cachePolicy] param is not given. | ||
-- check point: response code == 200 | ||
function testHttpDoAsyncRequestWithoutEnableRedirectAndCachePolicyParam() | ||
_G["respCode"] = 0 | ||
_G["respBody"] = "" | ||
_G["respHeaders"] = {} | ||
hs.http.doAsyncRequest( | ||
'http://google.com', | ||
'GET', | ||
nil, | ||
{ ['accept-language'] = 'en', ['user-agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36', Accept = '*/*' }, | ||
_G["callback"] | ||
) | ||
|
||
return success() | ||
end | ||
|
||
function testHttpDoAsyncRequestWithRedirectionValues() | ||
if (type(_G["respCode"]) == "number" and type(_G["respBody"]) == "string" and type(_G["respHeaders"]) == "table") then | ||
-- check return code | ||
assertIsEqual(200, _G["respCode"]) | ||
assertGreaterThan(0, string.len(_G["respBody"])) | ||
return success() | ||
else | ||
return "Waiting for success..." | ||
end | ||
end | ||
|
||
-- check request should be redirected if [enableRedirect|cachePolicy] param is set to true as enableRedirect | ||
-- check point: response code == 200 | ||
function testHttpDoAsyncRequestWithRedirection() | ||
_G["respCode"] = 0 | ||
_G["respBody"] = "" | ||
_G["respHeaders"] = {} | ||
hs.http.doAsyncRequest( | ||
'http://google.com', | ||
'GET', | ||
nil, | ||
{ ['accept-language'] = 'en', ['user-agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36', Accept = '*/*' }, | ||
_G["callback"], | ||
true | ||
) | ||
|
||
return success() | ||
end | ||
|
||
function testHttpDoAsyncRequestWithoutRedirectionValues() | ||
if (type(_G["respCode"]) == "number" and type(_G["respBody"]) == "string" and type(_G["respHeaders"]) == "table") then | ||
-- check return code | ||
assertIsEqual(301, _G["respCode"]) | ||
return success() | ||
else | ||
return "Waiting for success..." | ||
end | ||
end | ||
|
||
-- check request should not be redirected if [enableRedirect|cachePolicy] param is set to false as enableRedirect | ||
-- check point: response code == 301 | ||
function testHttpDoAsyncRequestWithoutRedirection() | ||
_G["respCode"] = 0 | ||
_G["respBody"] = "" | ||
_G["respHeaders"] = {} | ||
hs.http.doAsyncRequest( | ||
'http://google.com', | ||
'GET', | ||
nil, | ||
{ ['accept-language'] = 'en', ['user-agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36', Accept = '*/*' }, | ||
_G["callback"], | ||
false | ||
) | ||
|
||
return success() | ||
end |