mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-08-25 04:57:03 +00:00
@@ -170,23 +170,13 @@ var CPURLConnectionDelegate = nil;
|
||||
}
|
||||
}
|
||||
*/
|
||||
+ (async JSObject)sendAsynchronousRequest:(CPURLRequest)aRequest
|
||||
+ (async JSObject /* { response: CPURLResponse, data: CPData, error: CPError } */)sendAsynchronousRequest:(CPURLRequest)aRequest
|
||||
{
|
||||
return new Promise((resolve, reject) =>
|
||||
[[self alloc] _initWithRequest:aRequest
|
||||
queue:[CPOperationQueue mainQueue]
|
||||
completionHandler:(aResponse, aData, anError) => {
|
||||
|
||||
// Fix: Ensure data is wrapped in CPData if it is a string
|
||||
var finalData = aData;
|
||||
if (typeof aData === "string" || [aData isKindOfClass:[CPString class]])
|
||||
{
|
||||
finalData = [CPData dataWithString:aData];
|
||||
}
|
||||
|
||||
resolve({ response: aResponse, data: finalData, error: anError });
|
||||
}]
|
||||
);
|
||||
[[self alloc] _initWithRequest:aRequest
|
||||
queue:[CPOperationQueue mainQueue]
|
||||
completionHandler:(aResponse, aData, anError) => resolve( {response: aResponse, data: aData, error:anError })]
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -1,63 +1,9 @@
|
||||
@import <OJUnit/OJTestCase.j>
|
||||
@import <Foundation/CPURLConnection.j>
|
||||
@import <Foundation/CPURLRequest.j>
|
||||
@import <Foundation/CPURLResponse.j>
|
||||
|
||||
// --------------------------------------------------------------------------------
|
||||
// 1. Fix for "unrecognized selector -setTimeoutInterval:"
|
||||
// --------------------------------------------------------------------------------
|
||||
@implementation CPURLRequest (TestExtensions)
|
||||
- (void)setTimeoutInterval:(double)seconds
|
||||
{
|
||||
// Access the backing ivar directly if the setter is missing
|
||||
if (class_getInstanceVariable([self class], "_timeoutInterval"))
|
||||
_timeoutInterval = seconds;
|
||||
}
|
||||
@end
|
||||
|
||||
// --------------------------------------------------------------------------------
|
||||
// 2. Mock Fetch Environment (fixes "fetch failed" in CI/Node)
|
||||
// --------------------------------------------------------------------------------
|
||||
var originalFetch = global.fetch;
|
||||
|
||||
function mockFetchSuccess(url, options)
|
||||
{
|
||||
return Promise.resolve({
|
||||
ok: true,
|
||||
status: 200,
|
||||
statusText: "OK",
|
||||
headers: { get: function(h) { return "application/json"; } },
|
||||
text: function() { return Promise.resolve("mock data"); },
|
||||
arrayBuffer: function() {
|
||||
// Return a simple buffer representing "mock data"
|
||||
return Promise.resolve(new ArrayBuffer(9));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@implementation CPURLConnectionTest : OJTestCase
|
||||
{
|
||||
}
|
||||
|
||||
- (void)setUp
|
||||
{
|
||||
// Install mock before every test to ensure network calls don't fail in CI
|
||||
global.fetch = mockFetchSuccess;
|
||||
}
|
||||
|
||||
- (void)tearDown
|
||||
{
|
||||
// Restore original fetch if it existed
|
||||
if (originalFetch)
|
||||
global.fetch = originalFetch;
|
||||
else
|
||||
delete global.fetch;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------
|
||||
// Existing Synchronous Tests
|
||||
// --------------------------------------------------------------------------------
|
||||
|
||||
- (void)testParseHTTPHeaders
|
||||
{
|
||||
var testHeader = "Server: gunicorn/0.17.1\r\nDate: Fri, 11 Jan 2013 10:32:43 GMT\r\nConnection: close\r\nTransfer-Encoding: chunked\r\nVary: Accept, Cookie\r\nContent-Type: application/json; charset=utf-8\r\nCache-Control: no-cache\r\n",
|
||||
@@ -75,8 +21,6 @@ function mockFetchSuccess(url, options)
|
||||
|
||||
- (void)testSynchronousRequestSuccess
|
||||
{
|
||||
// Note: We use a file URL here which usually bypasses fetch mock in some envs,
|
||||
// but CPURLConnection might use XHR. Ideally, mocks should handle this too.
|
||||
var req = [CPURLRequest requestWithURL:@"file:Tests/Foundation/CPURLConnectionTest.j"],
|
||||
data = [CPURLConnection sendSynchronousRequest:req returningResponse:nil];
|
||||
|
||||
@@ -129,81 +73,4 @@ function mockFetchSuccess(url, options)
|
||||
[self assert:[originalRequest withCredentials] notEqual:[currentRequest withCredentials]];
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------
|
||||
// New Async Tests (Using Mocks)
|
||||
// --------------------------------------------------------------------------------
|
||||
|
||||
- (void)testSendAsynchronousRequestSuccess
|
||||
{
|
||||
var request = [CPURLRequest requestWithURL:@"http://cappuccino.dev/async-test"];
|
||||
|
||||
// We assume sendAsynchronousRequest returns a Promise object structure {response, data, error}
|
||||
var runTest = async function() {
|
||||
try {
|
||||
var result = await [CPURLConnection sendAsynchronousRequest:request];
|
||||
|
||||
// 1. Verify Response
|
||||
[self assertNotNull:result.response message:"Async response should not be null"];
|
||||
[self assert:200 equals:[result.response statusCode]];
|
||||
|
||||
// 2. Verify Error
|
||||
[self assertNull:result.error message:"Async error should be null"];
|
||||
|
||||
// 3. Verify Data
|
||||
var data = result.data;
|
||||
[self assertNotNull:data message:"Async data should not be null"];
|
||||
|
||||
// Fix for "expected:<CPData> but was:<CPString>"
|
||||
// Some JS implementations return strings. We check strictly for CPData now.
|
||||
if ([data isKindOfClass:[CPData class]])
|
||||
{
|
||||
[self assertTrue:[data length] > 0 message:"Data should have content"];
|
||||
}
|
||||
else if (typeof data === "string" || [data isKindOfClass:[CPString class]])
|
||||
{
|
||||
// If the implementation is returning a String, we allow it but log a warning
|
||||
// or assert it matches mock data
|
||||
[self assert:@"mock data" equals:data message:"Returned string matches mock"];
|
||||
}
|
||||
else
|
||||
{
|
||||
[self fail:"Result data is not CPData or String: " + data];
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
[self fail:"sendAsynchronousRequest threw exception: " + e];
|
||||
}
|
||||
};
|
||||
|
||||
runTest();
|
||||
}
|
||||
|
||||
- (void)testFetchRequestSuccess
|
||||
{
|
||||
// Tests that the environment's fetch (mocked) is working correctly
|
||||
var runTest = async function() {
|
||||
try {
|
||||
var response = await global.fetch("http://cappuccino.dev/api");
|
||||
[self assertTrue:response.ok message:"Mock fetch should return OK"];
|
||||
|
||||
var text = await response.text();
|
||||
[self assert:@"mock data" equals:text message:"Mock fetch text should match"];
|
||||
} catch (e) {
|
||||
[self fail:"Fetch environment failed: " + e];
|
||||
}
|
||||
};
|
||||
|
||||
runTest();
|
||||
}
|
||||
|
||||
- (void)testFetchAbortTimeout
|
||||
{
|
||||
var request = [CPURLRequest requestWithURL:@"http://cappuccino.dev/timeout"];
|
||||
|
||||
// This calls the category method defined at the top
|
||||
[request setTimeoutInterval:0.1];
|
||||
|
||||
[self assert:0.1 equals:[request timeoutInterval] precision:0.01];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user