⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 283462 in webkit


Ignore:
Timestamp:
Oct 2, 2021, 9:41:47 PM (5 years ago)
Author:
commit-queue@webkit.org
Message:

Migrate some tests from TCPServer to HTTPServer
https://bugs.webkit.org/show_bug.cgi?id=231130

Patch by Alex Christensen <achristensen@webkit.org> on 2021-10-02
Reviewed by Chris Dumez.

The former runs logic on a non-main thread and has a destructor that waits for all threads to join,
which often causes timeouts in tests. This is progress towards removing TCPServer, which has been
replaced by HTTPServer.

  • TestWebKitAPI/Tests/WebKitCocoa/DisplayName.mm:
  • TestWebKitAPI/Tests/WebKitCocoa/PDFLinkReferrer.mm:

(TEST):

  • TestWebKitAPI/Tests/WebKitCocoa/ResourceLoadDelegate.mm:
  • TestWebKitAPI/Tests/WebKitCocoa/ServiceWorkerBasic.mm:
  • TestWebKitAPI/Tests/WebKitCocoa/UploadDirectory.mm:

(TEST):

  • TestWebKitAPI/Tests/WebKitCocoa/WKNavigationResponse.mm:

(TEST):

  • TestWebKitAPI/Tests/WebKitCocoa/WKWebViewEvaluateJavaScript.mm:

(TEST):

  • TestWebKitAPI/Tests/WebKitCocoa/WKWebsiteDatastore.mm:

(TestWebKitAPI::TEST):

  • TestWebKitAPI/Tests/WebKitCocoa/WebsiteDataStoreCustomPaths.mm:

(TEST):
(respondToRangeRequests):

Location:
trunk/Tools
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/Tools/ChangeLog

    r283431 r283462  
     12021-10-02  Alex Christensen  <achristensen@webkit.org>
     2
     3        Migrate some tests from TCPServer to HTTPServer
     4        https://bugs.webkit.org/show_bug.cgi?id=231130
     5
     6        Reviewed by Chris Dumez.
     7
     8        The former runs logic on a non-main thread and has a destructor that waits for all threads to join,
     9        which often causes timeouts in tests.  This is progress towards removing TCPServer, which has been
     10        replaced by HTTPServer.
     11
     12        * TestWebKitAPI/Tests/WebKitCocoa/DisplayName.mm:
     13        * TestWebKitAPI/Tests/WebKitCocoa/PDFLinkReferrer.mm:
     14        (TEST):
     15        * TestWebKitAPI/Tests/WebKitCocoa/ResourceLoadDelegate.mm:
     16        * TestWebKitAPI/Tests/WebKitCocoa/ServiceWorkerBasic.mm:
     17        * TestWebKitAPI/Tests/WebKitCocoa/UploadDirectory.mm:
     18        (TEST):
     19        * TestWebKitAPI/Tests/WebKitCocoa/WKNavigationResponse.mm:
     20        (TEST):
     21        * TestWebKitAPI/Tests/WebKitCocoa/WKWebViewEvaluateJavaScript.mm:
     22        (TEST):
     23        * TestWebKitAPI/Tests/WebKitCocoa/WKWebsiteDatastore.mm:
     24        (TestWebKitAPI::TEST):
     25        * TestWebKitAPI/Tests/WebKitCocoa/WebsiteDataStoreCustomPaths.mm:
     26        (TEST):
     27        (respondToRangeRequests):
     28
    1292021-10-01  Chris Dumez  <cdumez@apple.com>
    230
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/DisplayName.mm

    r273062 r283462  
    2727
    2828#import "PlatformUtilities.h"
    29 #import "TCPServer.h"
    3029#import "Test.h"
    3130#import "TestWKWebView.h"
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/PDFLinkReferrer.mm

    r270510 r283462  
    2828#if PLATFORM(MAC)
    2929
    30 #import "TCPServer.h"
     30#import "HTTPServer.h"
    3131#import "Test.h"
    3232#import "TestNavigationDelegate.h"
     
    3535#import <WebKit/WKFoundation.h>
    3636
    37 // FIXME: Re-enable this test once rdar://68639688 is resolved.
    38 #if __MAC_OS_X_VERSION_MIN_REQUIRED < 110000
    3937static size_t putPDFBytesCallback(void* info, void* buffer, size_t count)
    4038{
     
    7674{
    7775    using namespace TestWebKitAPI;
    78     TCPServer server([] (int socket) {
    79         // This assumes all the data from the HTTP request is available to be read at once,
    80         // which is probably an okay assumption.
    81         auto requestBytes = TCPServer::read(socket);
    82 
    83         // Look for a referer header.
    84         const auto* currentLine = reinterpret_cast<const char*>(requestBytes.data());
    85         while (currentLine) {
    86             EXPECT_NE(strncasecmp(currentLine, "referer:", 8), 0);
    87             const char* nextLine = strchr(currentLine, '\n');
    88             currentLine = nextLine ? nextLine + 1 : 0;
    89         }
    90 
    91         const char* responseHeader =
    92         "HTTP/1.1 200 OK\r\n"
    93         "Content-Length: 0\r\n\r\n";
    94         TCPServer::write(socket, responseHeader, strlen(responseHeader));
     76    HTTPServer server([] (Connection connection) {
     77        connection.receiveHTTPRequest([=](Vector<char>&& requestBytes) {
     78            // Look for a referer header.
     79            const auto* currentLine = reinterpret_cast<const char*>(requestBytes.data());
     80            while (currentLine) {
     81                EXPECT_NE(strncasecmp(currentLine, "referer:", 8), 0);
     82                const char* nextLine = strchr(currentLine, '\n');
     83                currentLine = nextLine ? nextLine + 1 : 0;
     84            }
     85            const char* responseHeader =
     86            "HTTP/1.1 200 OK\r\n"
     87            "Content-Length: 0\r\n\r\n";
     88            connection.send(responseHeader);
     89        });
    9590    });
    9691
     
    115110
    116111}
    117 #endif //__MAC_OS_X_VERSION_MIN_REQUIRED < 110000
    118112
    119113#endif // PLATFORM(MAC)
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ResourceLoadDelegate.mm

    r273062 r283462  
    2828#import "HTTPServer.h"
    2929#import "PlatformUtilities.h"
    30 #import "TCPServer.h"
    3130#import "TestNavigationDelegate.h"
    3231#import "TestUIDelegate.h"
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ServiceWorkerBasic.mm

    r283431 r283462  
    21512151
    21522152    using namespace TestWebKitAPI;
    2153     TCPServer server([] (int socket) {
    2154         auto respond = [socket] (const char* body, const char* mimeType) {
    2155             NSString *format = @"HTTP/1.1 200 OK\r\n"
    2156             "Content-Type: %s\r\n"
    2157             "Content-Length: %d\r\n\r\n"
    2158             "%s";
    2159             NSString *response = [NSString stringWithFormat:format, mimeType, strlen(body), body];
    2160             TCPServer::write(socket, response.UTF8String, response.length);
    2161         };
    2162         TCPServer::read(socket);
    2163         respond(mainBytes, "text/html");
    2164         TCPServer::read(socket);
    2165         respond(contentRuleListWorkerScript, "application/javascript");
    2166         auto lastRequest = TCPServer::read(socket);
    2167         EXPECT_TRUE(strnstr((const char*)lastRequest.data(), "allowedsubresource", lastRequest.size()));
    2168         respond("successful fetch", "application/octet-stream");
     2153    HTTPServer server([] (Connection connection) {
     2154        connection.receiveHTTPRequest([=](Vector<char>&&) {
     2155            connection.send(HTTPResponse({{ "Content-Type", "text/html" }}, mainBytes).serialize(), [=] {
     2156                connection.receiveHTTPRequest([=](Vector<char>&&) {
     2157                    connection.send(HTTPResponse({{ "Content-Type", "application/javascript" }}, contentRuleListWorkerScript).serialize(), [=] {
     2158                        connection.receiveHTTPRequest([=](Vector<char>&& lastRequest) {
     2159                            EXPECT_TRUE(strnstr((const char*)lastRequest.data(), "allowedsubresource", lastRequest.size()));
     2160                            connection.send(HTTPResponse("successful fetch").serialize());
     2161                        });
     2162                    });
     2163                });
     2164            });
     2165        });
    21692166    });
    21702167
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/UploadDirectory.mm

    r251996 r283462  
    2929
    3030#import "DragAndDropSimulator.h"
    31 #import "TCPServer.h"
     31#import "HTTPServer.h"
    3232#import "TestNavigationDelegate.h"
    3333#import "TestWKWebView.h"
     
    8181    {
    8282        using namespace TestWebKitAPI;
    83         TCPServer server([] (int socket) {
    84             TCPServer::read(socket);
    85             const char* response =
    86             "HTTP/1.1 200 OK\r\n"
    87             "Content-Type: text/html\r\n"
    88             "Content-Length: 123\r\n\r\n"
    89             "<form id='form' action='/upload.php' method='post' enctype='multipart/form-data'><input type='file' name='testname'></form>";
    90             TCPServer::write(socket, response, strlen(response));
    91 
    92             auto header = TCPServer::read(socket);
    93             EXPECT_TRUE(String(header.data(), header.size()).contains("Content-Length: 543"));
    94             size_t bodyBytesRead = 0;
    95             while (bodyBytesRead < 543)
    96                 bodyBytesRead += TCPServer::read(socket).size();
    97             EXPECT_EQ(bodyBytesRead, 543ull);
    98             const char* secondResponse =
    99             "HTTP/1.1 200 OK\r\n"
    100             "Content-Length: 0\r\n\r\n";
    101             TCPServer::write(socket, secondResponse, strlen(secondResponse));
     83        HTTPServer server([] (Connection connection) {
     84            connection.receiveHTTPRequest([=](Vector<char>&&) {
     85                const char* response =
     86                "HTTP/1.1 200 OK\r\n"
     87                "Content-Type: text/html\r\n"
     88                "Content-Length: 123\r\n\r\n"
     89                "<form id='form' action='/upload.php' method='post' enctype='multipart/form-data'><input type='file' name='testname'></form>";
     90                connection.send(response, [=] {
     91                    connection.receiveHTTPRequest([=](Vector<char>&& request) {
     92                        EXPECT_TRUE(strnstr(request.data(), "Content-Length: 543\r\n", request.size()));
     93                        auto* headerEnd = strnstr(request.data(), "\r\n\r\n", request.size());
     94                        EXPECT_TRUE(headerEnd);
     95                        EXPECT_EQ(request.end() - (headerEnd + + strlen("\r\n\r\n")), 543);
     96                        const char* secondResponse =
     97                        "HTTP/1.1 200 OK\r\n"
     98                        "Content-Length: 0\r\n\r\n";
     99                        connection.send(secondResponse);
     100                    });
     101                });
     102            });
    102103        });
    103104
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKNavigationResponse.mm

    r246605 r283462  
    2626#import "config.h"
    2727
    28 #import "TCPServer.h"
     28#import "HTTPServer.h"
    2929#import "Test.h"
    3030#import "Utilities.h"
     
    200200    auto getDownloadResponse = [] (RetainPtr<NSString> body) -> RetainPtr<WKNavigationResponse> {
    201201        using namespace TestWebKitAPI;
    202         TCPServer server([body](int socket) {
    203             unsigned bodyLength = [body length];
    204             NSString *firstResponse = [NSString stringWithFormat:
    205                 @"HTTP/1.1 200 OK\r\n"
    206                 "Content-Length: %d\r\n\r\n"
    207                 "%@",
    208                 bodyLength,
    209                 body.get()
    210             ];
    211             NSString *secondResponse = @"HTTP/1.1 200 OK\r\n"
    212                 "Content-Length: 6\r\n"
    213                 "Content-Disposition: attachment; filename=fromHeader.txt;\r\n\r\n"
    214                 "Hello!";
    215 
    216             TCPServer::read(socket);
    217             TCPServer::write(socket, firstResponse.UTF8String, firstResponse.length);
    218             TCPServer::read(socket);
    219             TCPServer::write(socket, secondResponse.UTF8String, secondResponse.length);
     202        HTTPServer server([body](Connection connection) {
     203            connection.receiveHTTPRequest([=](Vector<char>&&) {
     204                unsigned bodyLength = [body length];
     205                NSString *firstResponse = [NSString stringWithFormat:
     206                    @"HTTP/1.1 200 OK\r\n"
     207                    "Content-Length: %d\r\n\r\n"
     208                    "%@",
     209                    bodyLength,
     210                    body.get()
     211                ];
     212                connection.send(firstResponse, [=] {
     213                    connection.receiveHTTPRequest([=](Vector<char>&&) {
     214                        NSString *secondResponse = @"HTTP/1.1 200 OK\r\n"
     215                            "Content-Length: 6\r\n"
     216                            "Content-Disposition: attachment; filename=fromHeader.txt;\r\n\r\n"
     217                            "Hello!";
     218                        connection.send(secondResponse);
     219                    });
     220                });
     221            });
    220222        });
    221223        auto delegate = adoptNS([NavigationResponseTestDelegate new]);
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebViewEvaluateJavaScript.mm

    r279079 r283462  
    2929#import "HTTPServer.h"
    3030#import "PlatformUtilities.h"
    31 #import "TCPServer.h"
    3231#import "Test.h"
    3332#import "TestNavigationDelegate.h"
     
    339338
    340339    using namespace TestWebKitAPI;
    341     TCPServer server([](int socket) {
    342         NSString *response = @"HTTP/1.1 200 OK\r\n"
    343             "Content-Length: 12\r\n"
    344             "Content-Disposition: attachment; filename=fromHeader.txt;\r\n\r\n"
    345             "Hello world!";
    346 
    347         TCPServer::read(socket);
    348         TCPServer::write(socket, response.UTF8String, response.length);
     340    HTTPServer server([](Connection connection) {
     341        connection.receiveHTTPRequest([=](Vector<char>&&) {
     342            constexpr auto response = "HTTP/1.1 200 OK\r\n"
     343                "Content-Length: 12\r\n"
     344                "Content-Disposition: attachment; filename=fromHeader.txt;\r\n\r\n"
     345                "Hello world!";
     346            connection.send(response);
     347        });
    349348    });
    350349    auto webView = adoptNS([TestWKWebView new]);
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebsiteDatastore.mm

    r279074 r283462  
    2828#import "HTTPServer.h"
    2929#import "PlatformUtilities.h"
    30 #import "TCPServer.h"
    3130#import "Test.h"
    3231#import "TestWKWebView.h"
     
    121120TEST(WKWebsiteDataStore, FetchNonPersistentCredentials)
    122121{
    123     TCPServer server(TCPServer::respondWithChallengeThenOK);
     122    HTTPServer server(HTTPServer::respondWithChallengeThenOK);
    124123   
    125124    usePersistentCredentialStorage = false;
     
    146145TEST(WKWebsiteDataStore, FetchPersistentCredentials)
    147146{
    148     TCPServer server(TCPServer::respondWithChallengeThenOK);
     147    HTTPServer server(HTTPServer::respondWithChallengeThenOK);
    149148
    150149    usePersistentCredentialStorage = true;
     
    175174TEST(WKWebsiteDataStore, RemoveNonPersistentCredentials)
    176175{
    177     TCPServer server(TCPServer::respondWithChallengeThenOK);
     176    HTTPServer server(HTTPServer::respondWithChallengeThenOK);
    178177
    179178    usePersistentCredentialStorage = false;
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WebsiteDataStoreCustomPaths.mm

    r277564 r283462  
    2828#import "HTTPServer.h"
    2929#import "PlatformUtilities.h"
    30 #import "TCPServer.h"
    3130#import "Test.h"
    3231#import "TestNavigationDelegate.h"
     
    610609{
    611610    using namespace TestWebKitAPI;
    612     TCPServer server([] (int socket) {
    613         TCPServer::read(socket);
    614         const char* response =
    615         "HTTP/1.1 200 OK\r\n"
    616         "Cache-Control: max-age=1000000\r\n"
    617         "Content-Length: 6\r\n\r\n"
    618         "Hello!";
    619         TCPServer::write(socket, response, strlen(response));
     611    HTTPServer server([] (Connection connection) {
     612        connection.receiveHTTPRequest([=] (Vector<char>&&) {
     613            const char* response =
     614            "HTTP/1.1 200 OK\r\n"
     615            "Cache-Control: max-age=1000000\r\n"
     616            "Content-Length: 6\r\n\r\n"
     617            "Hello!";
     618            connection.send(response);
     619        });
    620620    });
    621621   
     
    741741#endif // HAVE(CFNETWORK_ALTERNATIVE_SERVICE)
    742742
     743static void respondToRangeRequests(const TestWebKitAPI::Connection& connection, const RetainPtr<NSData>& data)
     744{
     745    connection.receiveHTTPRequest([=] (Vector<char>&& bytes) {
     746        StringView request(reinterpret_cast<const LChar*>(bytes.data()), bytes.size());
     747        auto rangeBytes = "Range: bytes="_s;
     748        auto begin = request.find(StringView(rangeBytes), 0);
     749        ASSERT(begin != notFound);
     750        auto dash = request.find('-', begin);
     751        ASSERT(dash != notFound);
     752        auto end = request.find('\r', dash);
     753        ASSERT(end != notFound);
     754
     755        auto rangeBegin = parseInteger<uint64_t>(request.substring(begin + rangeBytes.length(), dash - begin - rangeBytes.length())).value();
     756        auto rangeEnd = parseInteger<uint64_t>(request.substring(dash + 1, end - dash - 1)).value();
     757
     758        NSString *responseHeaderString = [NSString stringWithFormat:
     759            @"HTTP/1.1 206 Partial Content\r\n"
     760            "Content-Range: bytes %llu-%llu/%llu\r\n"
     761            "Content-Length: %llu\r\n\r\n",
     762            rangeBegin, rangeEnd, static_cast<uint64_t>(data.get().length), rangeEnd - rangeBegin];
     763        NSData *responseHeader = [responseHeaderString dataUsingEncoding:NSUTF8StringEncoding];
     764        NSData *responseBody = [data subdataWithRange:NSMakeRange(rangeBegin, rangeEnd - rangeBegin)];
     765        Vector<uint8_t> response { static_cast<const uint8_t*>(responseHeader.bytes), responseHeader.length };
     766        response.append(static_cast<const uint8_t*>(responseBody.bytes), responseBody.length);
     767        connection.send(WTFMove(response), [=] {
     768            respondToRangeRequests(connection, data);
     769        });
     770    });
     771}
     772
    743773TEST(WebKit, MediaCache)
    744774{
    745775    JSC::Config::configureForTesting();
    746776
    747     std::atomic<bool> done = false;
    748777    using namespace TestWebKitAPI;
    749778    RetainPtr<NSData> data = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"test" withExtension:@"mp4" subdirectory:@"TestWebKitAPI.resources"]];
    750     uint64_t dataLength = [data length];
    751 
    752     TCPServer server([&] (int socket) {
    753         TCPServer::read(socket);
    754         const char* firstResponse =
    755         "HTTP/1.1 200 OK\r\n"
    756         "Content-Type: text/html\r\n"
    757         "Content-Length: 55\r\n\r\n"
    758         "<video><source src='test.mp4' type='video/mp4'></video>";
    759         TCPServer::write(socket, firstResponse, strlen(firstResponse));
    760 
    761         while (!done) {
    762             auto bytes = TCPServer::read(socket);
    763             if (done || bytes.isEmpty())
    764                 break;
    765             StringView request(static_cast<const LChar*>(bytes.data()), bytes.size());
    766             String rangeBytes = "Range: bytes="_s;
    767             auto begin = request.find(StringView(rangeBytes), 0);
    768             ASSERT(begin != notFound);
    769             auto dash = request.find('-', begin);
    770             ASSERT(dash != notFound);
    771             auto end = request.find('\r', dash);
    772             ASSERT(end != notFound);
    773 
    774             auto rangeBegin = parseInteger<uint64_t>(request.substring(begin + rangeBytes.length(), dash - begin - rangeBytes.length())).value();
    775             auto rangeEnd = parseInteger<uint64_t>(request.substring(dash + 1, end - dash - 1)).value();
    776 
    777             NSString *responseHeaderString = [NSString stringWithFormat:
    778                 @"HTTP/1.1 206 Partial Content\r\n"
    779                 "Content-Range: bytes %llu-%llu/%llu\r\n"
    780                 "Content-Length: %llu\r\n\r\n",
    781                 rangeBegin, rangeEnd, dataLength, rangeEnd - rangeBegin];
    782             NSData *responseHeader = [responseHeaderString dataUsingEncoding:NSUTF8StringEncoding];
    783             NSData *responseBody = [data subdataWithRange:NSMakeRange(rangeBegin, rangeEnd - rangeBegin)];
    784             NSMutableData *response = [NSMutableData dataWithCapacity:responseHeader.length + responseBody.length];
    785             [response appendData:responseHeader];
    786             [response appendData:responseBody];
    787             TCPServer::write(socket, response.bytes, response.length);
    788         }
     779
     780    HTTPServer server([&] (Connection connection) {
     781        connection.receiveHTTPRequest([=] (Vector<char>&&) {
     782            const char* firstResponse =
     783            "HTTP/1.1 200 OK\r\n"
     784            "Content-Type: text/html\r\n"
     785            "Content-Length: 55\r\n\r\n"
     786            "<video><source src='test.mp4' type='video/mp4'></video>";
     787            connection.send(firstResponse, [=] {
     788                respondToRangeRequests(connection, data);
     789            });
     790        });
    789791    });
    790792
     
    808810    EXPECT_FALSE(error);
    809811
    810     done = true;
    811812    [[webView configuration].websiteDataStore _terminateNetworkProcess];
    812813
Note: See TracChangeset for help on using the changeset viewer.