[WININET_WINETEST] Sync with Wine Staging 1.9.23. CORE-12409
[reactos.git] / rostests / winetests / wininet / http.c
index bbffe27..741bd0c 100644 (file)
 
 #include <wine/test.h>
 
+/* Undocumented security flags */
+#define _SECURITY_FLAG_CERT_REV_FAILED    0x00800000
+#define _SECURITY_FLAG_CERT_INVALID_CA    0x01000000
+#define _SECURITY_FLAG_CERT_INVALID_CN    0x02000000
+#define _SECURITY_FLAG_CERT_INVALID_DATE  0x04000000
+
 #define TEST_URL "http://test.winehq.org/tests/hello.html"
 
 static BOOL first_connection_to_test_url = TRUE;
@@ -78,7 +84,7 @@ static BOOL first_connection_to_test_url = TRUE;
                status < MAX_INTERNET_STATUS && status_string[status] ? \
                status_string[status] : "unknown");                      \
             if (expect[status]) expect[status]--; \
-            else optional[status]--; \
+            else if(optional[status]) optional[status]--; \
         } \
         notified[status]++; \
     }while(0)
@@ -108,8 +114,9 @@ static int expect[MAX_INTERNET_STATUS], optional[MAX_INTERNET_STATUS],
     wine_allow[MAX_INTERNET_STATUS], notified[MAX_INTERNET_STATUS];
 static const char *status_string[MAX_INTERNET_STATUS];
 
-static HANDLE hCompleteEvent, conn_close_event;
+static HANDLE hCompleteEvent, conn_close_event, conn_wait_event, server_req_rec_event;
 static DWORD req_error;
+static BOOL is_ie7plus = TRUE;
 
 #define TESTF_REDIRECT      0x01
 #define TESTF_COMPRESSED    0x02
@@ -144,10 +151,10 @@ static const test_data_t test_data[] = {
         TESTF_REDIRECT
     },
     {
-        "http://www.codeweavers.com/",
-        "http://www.codeweavers.com/",
-        "www.codeweavers.com",
-        "",
+        "http://test.winehq.org/tests/gzip.php",
+        "http://test.winehq.org/tests/gzip.php",
+        "test.winehq.org",
+        "/tests/gzip.php",
         "Accept-Encoding: gzip, deflate",
         TESTF_COMPRESSED
     },
@@ -198,7 +205,7 @@ static BOOL proxy_active(void)
 static void _test_status_code(unsigned line, HINTERNET req, DWORD excode, BOOL is_todo)
 {
     DWORD code, size, index;
-    char exbuf[10], bufa[10];
+    char exbuf[12], bufa[10];
     WCHAR bufw[10];
     BOOL res;
 
@@ -206,9 +213,7 @@ static void _test_status_code(unsigned line, HINTERNET req, DWORD excode, BOOL i
     size = sizeof(code);
     res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &code, &size, NULL);
     ok_(__FILE__,line)(res, "[1] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE|number) failed: %u\n", GetLastError());
-    if (is_todo)
-        todo_wine ok_(__FILE__,line)(code == excode, "code = %d, expected %d\n", code, excode);
-    else
+    todo_wine_if (is_todo)
         ok_(__FILE__,line)(code == excode, "code = %d, expected %d\n", code, excode);
     ok_(__FILE__,line)(size == sizeof(code), "size = %u\n", size);
 
@@ -217,10 +222,7 @@ static void _test_status_code(unsigned line, HINTERNET req, DWORD excode, BOOL i
     size = sizeof(code);
     res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &code, &size, &index);
     ok_(__FILE__,line)(res, "[2] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE|number index) failed: %u\n", GetLastError());
-    if (is_todo)
-        todo_wine ok_(__FILE__,line)(code == excode, "code = %d, expected %d\n", code, excode);
-    else
-        ok_(__FILE__,line)(!index, "index = %d, expected 0\n", code);
+    ok_(__FILE__,line)(!index, "index = %d, expected 0\n", index);
     ok_(__FILE__,line)(size == sizeof(code), "size = %u\n", size);
 
     sprintf(exbuf, "%u", excode);
@@ -228,9 +230,7 @@ static void _test_status_code(unsigned line, HINTERNET req, DWORD excode, BOOL i
     size = sizeof(bufa);
     res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE, bufa, &size, NULL);
     ok_(__FILE__,line)(res, "[3] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
-    if (is_todo)
-        todo_wine ok_(__FILE__,line)(!strcmp(bufa, exbuf), "unexpected status code %s, expected %s\n", bufa, exbuf);
-    else
+    todo_wine_if (is_todo)
         ok_(__FILE__,line)(!strcmp(bufa, exbuf), "unexpected status code %s, expected %s\n", bufa, exbuf);
     ok_(__FILE__,line)(size == strlen(exbuf), "unexpected size %d for \"%s\"\n", size, exbuf);
 
@@ -243,9 +243,7 @@ static void _test_status_code(unsigned line, HINTERNET req, DWORD excode, BOOL i
     size = sizeof(bufw);
     res = HttpQueryInfoW(req, HTTP_QUERY_STATUS_CODE, bufw, &size, NULL);
     ok_(__FILE__,line)(res, "[5] HttpQueryInfoW(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
-    if (is_todo)
-        todo_wine ok_(__FILE__,line)(!strcmp_wa(bufw, exbuf), "unexpected status code %s, expected %s\n", bufa, exbuf);
-    else
+    todo_wine_if (is_todo)
         ok_(__FILE__,line)(!strcmp_wa(bufw, exbuf), "unexpected status code %s, expected %s\n", bufa, exbuf);
     ok_(__FILE__,line)(size == strlen(exbuf)*sizeof(WCHAR), "unexpected size %d for \"%s\"\n", size, exbuf);
 
@@ -290,10 +288,8 @@ static void _test_request_flags(unsigned line, HINTERNET req, DWORD exflags, BOO
 
     /* FIXME: Remove once we have INTERNET_REQFLAG_CACHE_WRITE_DISABLED implementation */
     flags &= ~INTERNET_REQFLAG_CACHE_WRITE_DISABLED;
-    if(!is_todo)
+    todo_wine_if (is_todo)
         ok_(__FILE__,line)(flags == exflags, "flags = %x, expected %x\n", flags, exflags);
-    else
-        todo_wine ok_(__FILE__,line)(flags == exflags, "flags = %x, expected %x\n", flags, exflags);
 }
 
 #define test_http_version(a) _test_http_version(__LINE__,a)
@@ -452,6 +448,51 @@ static VOID WINAPI callback(
     }
 }
 
+typedef struct {
+    HINTERNET session;
+    HINTERNET connection;
+    HINTERNET request;
+} test_request_t;
+
+#define open_simple_request(a,b,c,d,e) _open_simple_request(__LINE__,a,b,c,d,e)
+static void _open_simple_request(unsigned line, test_request_t *req, const char *host,
+        int port, const char *verb, const char *url)
+{
+    req->session = InternetOpenA(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
+    ok_(__FILE__,line)(req->session != NULL, "InternetOpenA failed: %u\n", GetLastError());
+
+    req->connection = InternetConnectA(req->session, host, port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
+    ok_(__FILE__,line)(req->connection != NULL, "InternetConnectA failed: %u\n", GetLastError());
+
+    req->request = HttpOpenRequestA(req->connection, verb, url, NULL, NULL, NULL, 0, 0);
+    ok_(__FILE__,line)(req->request != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
+}
+
+#define close_request(a) _close_request(__LINE__,a)
+static void _close_request(unsigned line, test_request_t *req)
+{
+    BOOL ret;
+
+    ret = InternetCloseHandle(req->request);
+    ok_(__FILE__,line)(ret, "InternetCloseHandle(request) failed: %u\n", GetLastError());
+    ret = InternetCloseHandle(req->connection);
+    ok_(__FILE__,line)(ret, "InternetCloseHandle(connection) failed: %u\n", GetLastError());
+    ret = InternetCloseHandle(req->session);
+    ok_(__FILE__,line)(ret, "InternetCloseHandle(session) failed: %u\n", GetLastError());
+}
+
+#define receive_simple_request(a,b,c) _receive_simple_request(__LINE__,a,b,c)
+static DWORD _receive_simple_request(unsigned line, HINTERNET req, char *buf, size_t buf_size)
+{
+    DWORD read = 0;
+    BOOL ret;
+
+    ret = InternetReadFile(req, buf, buf_size, &read);
+    ok_(__FILE__,line)(ret, "InternetReadFile failed: %u\n", GetLastError());
+
+    return read;
+}
+
 static void close_async_handle(HINTERNET handle, HANDLE complete_event, int handle_cnt)
 {
     BOOL res;
@@ -470,7 +511,8 @@ static void InternetReadFile_test(int flags, const test_data_t *test)
     char *post_data = NULL;
     BOOL res, on_async = TRUE;
     CHAR buffer[4000];
-    DWORD length, exlen = 0, post_len = 0;
+    WCHAR wbuffer[4000];
+    DWORD length, length2, index, exlen = 0, post_len = 0;
     const char *types[2] = { "*", NULL };
     HINTERNET hi, hic = 0, hor = 0;
 
@@ -529,7 +571,7 @@ static void InternetReadFile_test(int flags, const test_data_t *test)
     length = sizeof(buffer);
     res = HttpQueryInfoA(hor, HTTP_QUERY_RAW_HEADERS, buffer, &length, 0x0);
     ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
-    ok(length == 0, "HTTP_QUERY_RAW_HEADERS: expected length 0, but got %d\n", length);
+    ok(length == 0 || (length == 1 && !*buffer) /* win10 */, "HTTP_QUERY_RAW_HEADERS: expected length 0, but got %d\n", length);
     ok(!strcmp(buffer, ""), "HTTP_QUERY_RAW_HEADERS: expected string \"\", but got \"%s\"\n", buffer);
 
     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
@@ -631,21 +673,82 @@ static void InternetReadFile_test(int flags, const test_data_t *test)
     ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed with error %d\n", GetLastError());
 
     length = sizeof(buffer)-1;
+    memset(buffer, 0x77, sizeof(buffer));
     res = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
     ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
-    buffer[length]=0;
+    /* show that the function writes data past the length returned */
+    ok(buffer[length-2], "Expected any header character, got 0x00\n");
+    ok(!buffer[length-1], "Expected 0x00, got %02X\n", buffer[length-1]);
+    ok(!buffer[length], "Expected 0x00, got %02X\n", buffer[length]);
+    ok(buffer[length+1] == 0x77, "Expected 0x77, got %02X\n", buffer[length+1]);
+
+    length2 = length;
+    res = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length2,0x0);
+    ok(!res, "Expected 0x00, got %d\n", res);
+    ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
+    ok(length2 == length+1, "Expected %d, got %d\n", length+1, length2);
+    /* the in length of the buffer must be +1 but the length returned does not count this */
+    length2 = length+1;
+    memset(buffer, 0x77, sizeof(buffer));
+    res = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length2,0x0);
+    ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
+    ok(buffer[length2] == 0x00, "Expected 0x00, got %02X\n", buffer[length2]);
+    ok(buffer[length2+1] == 0x77, "Expected 0x77, got %02X\n", buffer[length2+1]);
+    ok(length2 == length, "Value should not have changed: %d != %d\n", length2, length);
+
+    length = sizeof(wbuffer)-sizeof(WCHAR);
+    memset(wbuffer, 0x77, sizeof(wbuffer));
+    res = HttpQueryInfoW(hor, HTTP_QUERY_RAW_HEADERS, wbuffer, &length, 0x0);
+    ok(res, "HttpQueryInfoW(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
+    ok(length % sizeof(WCHAR) == 0, "Expected that length is a multiple of sizeof(WCHAR), got %d.\n", length);
+    length /= sizeof(WCHAR);
+    /* show that the function writes data past the length returned */
+    ok(wbuffer[length-2], "Expected any header character, got 0x0000\n");
+    ok(!wbuffer[length-1], "Expected 0x0000, got %04X\n", wbuffer[length-1]);
+    ok(!wbuffer[length], "Expected 0x0000, got %04X\n", wbuffer[length]);
+    ok(wbuffer[length+1] == 0x7777 || broken(wbuffer[length+1] != 0x7777),
+       "Expected 0x7777, got %04X\n", wbuffer[length+1]);
+
+    length2 = length*sizeof(WCHAR);
+    res = HttpQueryInfoW(hor,HTTP_QUERY_RAW_HEADERS,wbuffer,&length2,0x0);
+    ok(!res, "Expected 0x00, got %d\n", res);
+    ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
+    ok(length2 % sizeof(WCHAR) == 0, "Expected that length is a multiple of sizeof(WCHAR), got %d.\n", length2);
+    length2 /= sizeof(WCHAR);
+    ok(length2 == length+1, "Expected %d, got %d\n", length+1, length2);
+    /* the in length of the buffer must be +1 but the length returned does not count this */
+    length2 = (length+1)*sizeof(WCHAR);
+    memset(wbuffer, 0x77, sizeof(wbuffer));
+    res = HttpQueryInfoW(hor,HTTP_QUERY_RAW_HEADERS,wbuffer,&length2,0x0);
+    ok(res, "HttpQueryInfoW(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
+    ok(length2 % sizeof(WCHAR) == 0, "Expected that length is a multiple of sizeof(WCHAR), got %d.\n", length2);
+    length2 /= sizeof(WCHAR);
+    ok(!wbuffer[length2], "Expected 0x0000, got %04X\n", wbuffer[length2]);
+    ok(wbuffer[length2+1] == 0x7777, "Expected 0x7777, got %04X\n", wbuffer[length2+1]);
+    ok(length2 == length, "Value should not have changed: %d != %d\n", length2, length);
 
     length = sizeof(buffer);
     res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
     ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
     ok(!strcmp(buffer, test->redirected_url), "Wrong URL %s\n", buffer);
 
+    index = 0;
+    length = 0;
+    SetLastError(0xdeadbeef);
+    ok(HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,NULL,&length,&index) == FALSE,"Query worked\n");
+    if(test->flags & TESTF_COMPRESSED)
+        ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
+           "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", GetLastError());
+    ok(index == 0, "Index was incremented\n");
+
+    index = 0;
     length = 16;
-    res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
+    res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,&index);
     trace("Option HTTP_QUERY_CONTENT_LENGTH -> %i  %s  (%u)\n",res,buffer,GetLastError());
     if(test->flags & TESTF_COMPRESSED)
         ok(!res && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
            "expected ERROR_HTTP_HEADER_NOT_FOUND, got %x (%u)\n", res, GetLastError());
+    ok(!res || index == 1, "Index was not incremented although result is %x (index = %u)\n", res, index);
 
     length = 100;
     res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
@@ -671,8 +774,16 @@ static void InternetReadFile_test(int flags, const test_data_t *test)
     {
         if (flags & INTERNET_FLAG_ASYNC)
             SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
+
+        /* IE11 calls those in InternetQueryDataAvailable call. */
+        SET_OPTIONAL(INTERNET_STATUS_RECEIVING_RESPONSE);
+        SET_OPTIONAL(INTERNET_STATUS_RESPONSE_RECEIVED);
+
         length = 0;
         res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
+
+        CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
+
         if (flags & INTERNET_FLAG_ASYNC)
         {
             if (res)
@@ -693,6 +804,7 @@ static void InternetReadFile_test(int flags, const test_data_t *test)
                 exlen = length;
                 ok(exlen, "length = 0\n");
                 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
+                CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
                 ok(req_error, "req_error = 0\n");
                 continue;
             }else {
@@ -701,6 +813,8 @@ static void InternetReadFile_test(int flags, const test_data_t *test)
         }else {
             ok(res, "InternetQueryDataAvailable failed: %u\n", GetLastError());
         }
+        CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
+
         trace("LENGTH %d\n", length);
         if(test->flags & TESTF_CHUNKED)
             ok(length <= 8192, "length = %d, expected <= 8192\n", length);
@@ -971,6 +1085,13 @@ static void InternetReadFileExA_test(int flags)
     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
 
+    if(is_ie7plus) {
+        rc = InternetReadFileExW(hor, NULL, 0, 0xdeadcafe);
+        ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
+           "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
+           rc ? "TRUE" : "FALSE", GetLastError());
+    }
+
     /* tests invalid dwStructSize */
     inetbuffers.dwStructSize = sizeof(inetbuffers)+1;
     inetbuffers.lpcszHeader = NULL;
@@ -1036,7 +1157,7 @@ static void InternetReadFileExA_test(int flags)
                 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
                 WaitForSingleObject(hCompleteEvent, INFINITE);
                 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
-                CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
+                CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
                 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
             }
             else
@@ -1142,11 +1263,11 @@ static void HttpSendRequestEx_test(void)
     hSession = InternetOpenA("Wine Regression Test",
             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
     ok( hSession != NULL ,"Unable to open Internet session\n");
-    hConnect = InternetConnectA(hSession, "crossover.codeweavers.com",
+    hConnect = InternetConnectA(hSession, "test.winehq.org",
             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
             0);
-    ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
-    hRequest = HttpOpenRequestA(hConnect, "POST", "/posttest.php",
+    ok( hConnect != NULL, "Unable to connect to http://test.winehq.org\n");
+    hRequest = HttpOpenRequestA(hConnect, "POST", "/tests/post.php",
             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
     if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
     {
@@ -1358,7 +1479,9 @@ static void test_http_cache(void)
     BYTE buf[100];
     HANDLE file;
     BOOL ret;
+    FILETIME filetime_zero = {0};
 
+    static const char cached_content[] = "data read from cache";
     static const char *types[] = { "*", "", NULL };
 
     session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
@@ -1418,9 +1541,13 @@ static void test_http_cache(void)
 
     ok(InternetCloseHandle(request), "Close request handle failed\n");
 
-    file = CreateFileA(file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING,
-                      FILE_ATTRIBUTE_NORMAL, NULL);
+    file = CreateFileA(file_name, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
+            FILE_ATTRIBUTE_NORMAL, NULL);
     ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
+    ret = WriteFile(file, cached_content, sizeof(cached_content), &size, NULL);
+    ok(ret && size, "WriteFile failed: %d, %d\n", ret, size);
+    ret = CommitUrlCacheEntryA(url, file_name, filetime_zero, filetime_zero, NORMAL_CACHE_ENTRY, NULL, 0, NULL, 0);
+    ok(ret, "CommitUrlCacheEntry failed: %d\n", GetLastError());
     CloseHandle(file);
 
     /* Send the same request, requiring it to be retrieved from the cache */
@@ -1433,7 +1560,17 @@ static void test_http_cache(void)
     ret = InternetReadFile(request, buf, sizeof(buf), &size);
     ok(ret, "InternetReadFile failed: %u\n", GetLastError());
     ok(size == 100, "size = %u\n", size);
+    buf[99] = 0;
+    todo_wine ok(!strcmp((char*)buf, cached_content), "incorrect page data: %s\n", (char*)buf);
+
+    ok(InternetCloseHandle(request), "Close request handle failed\n");
 
+    DeleteUrlCacheEntryA(url);
+    request = HttpOpenRequestA(connect, "GET", "/tests/hello.html", NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
+    ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
+    todo_wine ok(!ret, "HttpSendRequest succeeded\n");
+    if(!ret)
+        ok(GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError() = %d\n", GetLastError());
     ok(InternetCloseHandle(request), "Close request handle failed\n");
 
     request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
@@ -1473,55 +1610,36 @@ static void test_http_cache(void)
 
 static void InternetLockRequestFile_test(void)
 {
-    HINTERNET session, connect, request;
     char file_name[MAX_PATH];
+    test_request_t req;
     HANDLE lock, lock2;
     DWORD size;
     BOOL ret;
 
-    static const char *types[] = { "*", "", NULL };
-
-    session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
-    ok(session != NULL ,"Unable to open Internet session\n");
-
-    connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
-                              INTERNET_SERVICE_HTTP, 0, 0);
-    ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
-
-    request = HttpOpenRequestA(connect, NULL, "/tests/hello.html", NULL, NULL, types, INTERNET_FLAG_NEED_FILE|INTERNET_FLAG_RELOAD, 0);
-    if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
-    {
-        skip( "Network unreachable, skipping test\n" );
-
-        ok(InternetCloseHandle(connect), "Close connect handle failed\n");
-        ok(InternetCloseHandle(session), "Close session handle failed\n");
-
-        return;
-    }
-    ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
+    open_simple_request(&req, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, "/tests/hello.html");
 
     size = sizeof(file_name);
-    ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
+    ret = InternetQueryOptionA(req.request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
     ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
     ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
     ok(!size, "size = %d\n", size);
 
     lock = NULL;
-    ret = InternetLockRequestFile(request, &lock);
+    ret = InternetLockRequestFile(req.request, &lock);
     ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "InternetLockRequestFile returned: %x(%u)\n", ret, GetLastError());
 
-    ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
+    ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
 
     size = sizeof(file_name);
-    ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
+    ret = InternetQueryOptionA(req.request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
     ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
 
-    ret = InternetLockRequestFile(request, &lock);
+    ret = InternetLockRequestFile(req.request, &lock);
     ok(ret, "InternetLockRequestFile returned: %x(%u)\n", ret, GetLastError());
     ok(lock != NULL, "lock == NULL\n");
 
-    ret = InternetLockRequestFile(request, &lock2);
+    ret = InternetLockRequestFile(req.request, &lock2);
     ok(ret, "InternetLockRequestFile returned: %x(%u)\n", ret, GetLastError());
     ok(lock == lock2, "lock != lock2\n");
 
@@ -1531,7 +1649,7 @@ static void InternetLockRequestFile_test(void)
     ret = DeleteFileA(file_name);
     ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION, "Deleting file returned %x(%u)\n", ret, GetLastError());
 
-    ok(InternetCloseHandle(request), "Close request handle failed\n");
+    ok(InternetCloseHandle(req.request), "Close request handle failed\n");
 
     ret = DeleteFileA(file_name);
     ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION, "Deleting file returned %x(%u)\n", ret, GetLastError());
@@ -1558,11 +1676,11 @@ static void HttpHeaders_test(void)
     hSession = InternetOpenA("Wine Regression Test",
             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
     ok( hSession != NULL ,"Unable to open Internet session\n");
-    hConnect = InternetConnectA(hSession, "crossover.codeweavers.com",
+    hConnect = InternetConnectA(hSession, "test.winehq.org",
             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
             0);
-    ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
-    hRequest = HttpOpenRequestA(hConnect, "POST", "/posttest.php",
+    ok( hConnect != NULL, "Unable to connect to http://test.winehq.org\n");
+    hRequest = HttpOpenRequestA(hConnect, "POST", "/tests/post.php",
             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
     if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
     {
@@ -1599,6 +1717,8 @@ static void HttpHeaders_test(void)
     strcpy(buffer,"Warning");
     ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
                 buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
+    ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
+    ok(index == 0, "Index was incremented\n");
     ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
     ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
 
@@ -1643,7 +1763,7 @@ static void HttpHeaders_test(void)
     ok((len < sizeof(buffer)-sizeof(CHAR)) && (buffer[len/sizeof(CHAR)] == 0),"No NUL at end\n");
     ok(len == strlen(buffer) * sizeof(CHAR), "Length wrong\n");
     /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
-    ok(strncmp(buffer, "POST /posttest.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
+    ok(strncmp(buffer, "POST /tests/post.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
     ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
     ok(index == 0, "Index was incremented\n");
 
@@ -1678,8 +1798,8 @@ static void HttpHeaders_test(void)
     memset(buffer, 'x', sizeof(buffer));
     ok(HttpQueryInfoA(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF,
                 buffer,&len,&index) == TRUE,"Query failed\n");
-    ok(len == 2, "Expected 2, got %d\n", len);
-    ok(strcmp(buffer, "\r\n") == 0, "Expected CRLF, got '%s'\n", buffer);
+    ok(len == 2 || len == 4 /* win10 */, "Expected 2 or 4, got %d\n", len);
+    ok(memcmp(buffer, "\r\n\r\n", len) == 0, "Expected CRLF, got '%s'\n", buffer);
     ok(index == 0, "Index was incremented\n");
 
     ok(HttpAddRequestHeadersA(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
@@ -1881,8 +2001,7 @@ static const char contmsg[] =
 static const char expandcontmsg[] =
 "HTTP/1.1 100 Continue\r\n"
 "Server: winecontinue\r\n"
-"Tag: something witty\r\n"
-"\r\n";
+"Tag: something witty\r\n";
 
 static const char okmsg[] =
 "HTTP/1.1 200 OK\r\n"
@@ -1898,6 +2017,22 @@ static const char okmsg2[] =
 "Set-Cookie: two\r\n"
 "\r\n";
 
+static const char okmsg_cookie_path[] =
+"HTTP/1.1 200 OK\r\n"
+"Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
+"Server: winetest\r\n"
+"Content-Length: 0\r\n"
+"Set-Cookie: subcookie2=data; path=/test_cookie_set_path\r\n"
+"\r\n";
+
+static const char okmsg_cookie[] =
+"HTTP/1.1 200 OK\r\n"
+"Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
+"Server: winetest\r\n"
+"Content-Length: 0\r\n"
+"Set-Cookie: testcookie=testvalue\r\n"
+"\r\n";
+
 static const char notokmsg[] =
 "HTTP/1.1 400 Bad Request\r\n"
 "Server: winetest\r\n"
@@ -1948,18 +2083,20 @@ struct server_info {
 
 static int test_cache_gzip;
 static const char *send_buffer;
+static int server_socket;
 
 static DWORD CALLBACK server_thread(LPVOID param)
 {
     struct server_info *si = param;
-    int r, c, i, on, count = 0;
+    int r, c = -1, i, on, count = 0;
     SOCKET s;
     struct sockaddr_in sa;
-    char buffer[0x100];
+    char *buffer;
+    size_t buffer_size;
     WSADATA wsaData;
     int last_request = 0;
     char host_header[22];
-    static BOOL test_b = FALSE;
+    char host_header_override[30];
     static int test_no_cache = 0;
 
     WSAStartup(MAKEWORD(1,1), &wsaData);
@@ -1985,15 +2122,21 @@ static DWORD CALLBACK server_thread(LPVOID param)
     SetEvent(si->hEvent);
 
     sprintf(host_header, "Host: localhost:%d", si->port);
+    sprintf(host_header_override, "Host: test.local:%d\r\n", si->port);
+    buffer = HeapAlloc(GetProcessHeap(), 0, buffer_size = 1000);
 
     do
     {
-        c = accept(s, NULL, NULL);
+        if(c == -1)
+            c = accept(s, NULL, NULL);
 
-        memset(buffer, 0, sizeof buffer);
-        for(i=0; i<(sizeof buffer-1); i++)
+        memset(buffer, 0, buffer_size);
+        for(i=0;; i++)
         {
-            r = recv(c, &buffer[i], 1, 0);
+            if(i == buffer_size)
+                buffer = HeapReAlloc(GetProcessHeap(), 0, buffer, buffer_size *= 2);
+
+            r = recv(c, buffer+i, 1, 0);
             if (r != 1)
                 break;
             if (i<4) continue;
@@ -2011,6 +2154,13 @@ static DWORD CALLBACK server_thread(LPVOID param)
             else
                 send(c, notokmsg, sizeof notokmsg-1, 0);
         }
+        if (strstr(buffer, "CONNECT "))
+        {
+            if (!strstr(buffer, "Content-Length: 0"))
+                send(c, notokmsg, sizeof notokmsg-1, 0);
+            else
+                send(c, proxymsg, sizeof proxymsg-1, 0);
+        }
         if (strstr(buffer, "/test2"))
         {
             if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
@@ -2058,6 +2208,8 @@ static DWORD CALLBACK server_thread(LPVOID param)
         {
             if (strstr(buffer, "Content-Length: 100"))
             {
+                if (strstr(buffer, "POST /test7b"))
+                    recvfrom(c, buffer, buffer_size, 0, NULL, NULL);
                 send(c, okmsg, sizeof okmsg-1, 0);
                 send(c, page1, sizeof page1-1, 0);
             }
@@ -2097,16 +2249,9 @@ static DWORD CALLBACK server_thread(LPVOID param)
             else
                 send(c, notokmsg, sizeof notokmsg-1, 0);
         }
-        if (!test_b && strstr(buffer, "/testB HTTP/1.1"))
-        {
-            test_b = TRUE;
-            send(c, okmsg, sizeof okmsg-1, 0);
-            recvfrom(c, buffer, sizeof buffer, 0, NULL, NULL);
-            send(c, okmsg, sizeof okmsg-1, 0);
-        }
         if (strstr(buffer, "/testC"))
         {
-            if (strstr(buffer, "Cookie: cookie=biscuit"))
+            if (strstr(buffer, "cookie=biscuit"))
                 send(c, okmsg, sizeof okmsg-1, 0);
             else
                 send(c, notokmsg, sizeof notokmsg-1, 0);
@@ -2155,7 +2300,7 @@ static DWORD CALLBACK server_thread(LPVOID param)
         if (strstr(buffer, "GET /testH"))
         {
             send(c, ok_with_length2, sizeof(ok_with_length2)-1, 0);
-            recvfrom(c, buffer, sizeof(buffer), 0, NULL, NULL);
+            recvfrom(c, buffer, buffer_size, 0, NULL, NULL);
             send(c, ok_with_length, sizeof(ok_with_length)-1, 0);
         }
 
@@ -2194,6 +2339,16 @@ static DWORD CALLBACK server_thread(LPVOID param)
             else
                 send(c, notokmsg, sizeof(notokmsg)-1, 0);
         }
+        if (strstr(buffer, "HEAD /test_head")) {
+            static const char head_response[] =
+                "HTTP/1.1 200 OK\r\n"
+                "Connection: Keep-Alive\r\n"
+                "Content-Length: 100\r\n"
+                "\r\n";
+
+            send(c, head_response, sizeof(head_response), 0);
+            continue;
+        }
         if (strstr(buffer, "GET /send_from_buffer"))
             send(c, send_buffer, strlen(send_buffer), 0);
         if (strstr(buffer, "/test_cache_control_verb"))
@@ -2203,72 +2358,181 @@ static DWORD CALLBACK server_thread(LPVOID param)
             else if (strstr(buffer, "Cache-Control: no-cache\r\n")) send(c, okmsg, sizeof(okmsg)-1, 0);
             else send(c, notokmsg, sizeof(notokmsg)-1, 0);
         }
+        if (strstr(buffer, "/test_request_content_length"))
+        {
+            static char msg[] = "HTTP/1.1 200 OK\r\nConnection: Keep-Alive\r\n\r\n";
+            static int seen_content_length;
+
+            if (!seen_content_length)
+            {
+                if (strstr(buffer, "Content-Length: 0"))
+                {
+                    seen_content_length = 1;
+                    send(c, msg, sizeof msg-1, 0);
+                }
+                else send(c, notokmsg, sizeof notokmsg-1, 0);
+                WaitForSingleObject(hCompleteEvent, 5000);
+            }
+            else
+            {
+                if (strstr(buffer, "Content-Length: 0")) send(c, msg, sizeof msg-1, 0);
+                else send(c, notokmsg, sizeof notokmsg-1, 0);
+                WaitForSingleObject(hCompleteEvent, 5000);
+            }
+        }
         if (strstr(buffer, "GET /test_premature_disconnect"))
             trace("closing connection\n");
-
+        if (strstr(buffer, "HEAD /upload.txt"))
+        {
+            if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
+                send(c, okmsg, sizeof okmsg-1, 0);
+            else
+                send(c, noauthmsg, sizeof noauthmsg-1, 0);
+        }
+        if (strstr(buffer, "PUT /upload2.txt"))
+        {
+            if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
+                send(c, okmsg, sizeof okmsg-1, 0);
+            else
+                send(c, notokmsg, sizeof notokmsg-1, 0);
+        }
+        if (strstr(buffer, "/test_cookie_path1"))
+        {
+            if (strstr(buffer, "subcookie=data"))
+                 send(c, okmsg, sizeof okmsg-1, 0);
+             else
+                 send(c, notokmsg, sizeof notokmsg-1, 0);
+        }
+        if (strstr(buffer, "/test_cookie_path2"))
+        {
+            if (strstr(buffer, "subcookie2=data"))
+                 send(c, okmsg, sizeof okmsg-1, 0);
+             else
+                 send(c, notokmsg, sizeof notokmsg-1, 0);
+        }
+        if (strstr(buffer, "/test_cookie_set_path"))
+        {
+            send(c, okmsg_cookie_path, sizeof okmsg_cookie_path-1, 0);
+        }
+        if (strstr(buffer, "/test_cookie_merge"))
+        {
+            if (strstr(buffer, "subcookie=data") &&
+                !strstr(buffer, "manual_cookie=test"))
+                 send(c, okmsg, sizeof okmsg-1, 0);
+             else
+                 send(c, notokmsg, sizeof notokmsg-1, 0);
+        }
+        if (strstr(buffer, "/test_cookie_set_host_override"))
+        {
+            send(c, okmsg_cookie, sizeof okmsg_cookie-1, 0);
+        }
+        if (strstr(buffer, "/test_cookie_check_host_override"))
+        {
+            if (strstr(buffer, "Cookie:") && strstr(buffer, "testcookie=testvalue"))
+                send(c, okmsg, sizeof okmsg-1, 0);
+            else
+                send(c, notokmsg, sizeof notokmsg-1, 0);
+        }
+        if (strstr(buffer, "/test_cookie_check_different_host"))
+        {
+            if (!strstr(buffer, "foo") &&
+                strstr(buffer, "cookie=biscuit"))
+                send(c, okmsg, sizeof okmsg-1, 0);
+            else
+                send(c, notokmsg, sizeof notokmsg-1, 0);
+        }
+        if (strstr(buffer, "/test_host_override"))
+        {
+            if (strstr(buffer, host_header_override))
+                send(c, okmsg, sizeof okmsg-1, 0);
+            else
+                send(c, notokmsg, sizeof notokmsg-1, 0);
+        }
+        if (strstr(buffer, "/async_read"))
+        {
+            const char *page1_mid = page1 + (sizeof page1 - 1)/2;
+            const char *page1_end = page1 + sizeof page1 - 1;
+            send(c, okmsg, sizeof okmsg-1, 0);
+            send(c, page1, page1_mid - page1, 0);
+            WaitForSingleObject(conn_wait_event, INFINITE);
+            send(c, page1_mid, page1_end - page1_mid, 0);
+        }
+        if (strstr(buffer, "/socket"))
+        {
+            server_socket = c;
+            SetEvent(server_req_rec_event);
+            WaitForSingleObject(conn_wait_event, INFINITE);
+        }
+        if (strstr(buffer, "/echo_request"))
+        {
+            send(c, okmsg, sizeof(okmsg)-1, 0);
+            send(c, buffer, strlen(buffer), 0);
+        }
+        if (strstr(buffer, "HEAD /test_auth_host1"))
+        {
+            if (strstr(buffer, "Authorization: Basic dGVzdDE6cGFzcw=="))
+                send(c, okmsg, sizeof okmsg-1, 0);
+            else
+                send(c, noauthmsg, sizeof noauthmsg-1, 0);
+        }
+        if (strstr(buffer, "HEAD /test_auth_host2"))
+        {
+            if (strstr(buffer, "Authorization: Basic dGVzdDE6cGFzczI="))
+                send(c, okmsg, sizeof okmsg-1, 0);
+            else
+                send(c, noauthmsg, sizeof noauthmsg-1, 0);
+        }
         shutdown(c, 2);
         closesocket(c);
+        c = -1;
     } while (!last_request);
 
     closesocket(s);
+    HeapFree(GetProcessHeap(), 0, buffer);
 
     return 0;
 }
 
 static void test_basic_request(int port, const char *verb, const char *url)
 {
-    HINTERNET hi, hc, hr;
+    test_request_t req;
     DWORD r, count, error;
     char buffer[0x100];
 
-    hi = InternetOpenA(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
-    ok(hi != NULL, "open failed\n");
-
-    hc = InternetConnectA(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
-    ok(hc != NULL, "connect failed\n");
+    trace("basic request %s %s\n", verb, url);
 
-    hr = HttpOpenRequestA(hc, verb, url, NULL, NULL, NULL, 0, 0);
-    ok(hr != NULL, "HttpOpenRequest failed\n");
+    open_simple_request(&req, "localhost", port, verb, url);
 
     SetLastError(0xdeadbeef);
-    r = HttpSendRequestA(hr, NULL, 0, NULL, 0);
+    r = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
     error = GetLastError();
     ok(error == ERROR_SUCCESS || broken(error != ERROR_SUCCESS), "expected ERROR_SUCCESS, got %u\n", error);
-    ok(r, "HttpSendRequest failed\n");
+    ok(r, "HttpSendRequest failed: %u\n", GetLastError());
 
     count = 0;
     memset(buffer, 0, sizeof buffer);
     SetLastError(0xdeadbeef);
-    r = InternetReadFile(hr, buffer, sizeof buffer, &count);
+    r = InternetReadFile(req.request, buffer, sizeof buffer, &count);
     ok(r, "InternetReadFile failed %u\n", GetLastError());
     ok(count == sizeof page1 - 1, "count was wrong\n");
     ok(!memcmp(buffer, page1, sizeof page1), "http data wrong, got: %s\n", buffer);
 
-    InternetCloseHandle(hr);
-    InternetCloseHandle(hc);
-    InternetCloseHandle(hi);
+    close_request(&req);
 }
 
 static void test_proxy_indirect(int port)
 {
-    HINTERNET hi, hc, hr;
+    test_request_t req;
     DWORD r, sz;
     char buffer[0x40];
 
-    hi = InternetOpenA(NULL, 0, NULL, NULL, 0);
-    ok(hi != NULL, "open failed\n");
-
-    hc = InternetConnectA(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
-    ok(hc != NULL, "connect failed\n");
-
-    hr = HttpOpenRequestA(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
-    ok(hr != NULL, "HttpOpenRequest failed\n");
+    open_simple_request(&req, "localhost", port, NULL, "/test2");
 
-    r = HttpSendRequestA(hr, NULL, 0, NULL, 0);
+    r = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
     ok(r, "HttpSendRequest failed %u\n", GetLastError());
 
     sz = sizeof buffer;
-    r = HttpQueryInfoA(hr, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
+    r = HttpQueryInfoA(req.request, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
     ok(r || GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo failed: %d\n", GetLastError());
     if (!r)
     {
@@ -2277,33 +2541,31 @@ static void test_proxy_indirect(int port)
     }
     ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
 
-    test_status_code(hr, 407);
-    test_request_flags(hr, 0);
+    test_status_code(req.request, 407);
+    test_request_flags(req.request, 0);
 
     sz = sizeof buffer;
-    r = HttpQueryInfoA(hr, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
+    r = HttpQueryInfoA(req.request, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
     ok(r, "HttpQueryInfo failed\n");
     ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
 
     sz = sizeof buffer;
-    r = HttpQueryInfoA(hr, HTTP_QUERY_VERSION, buffer, &sz, NULL);
+    r = HttpQueryInfoA(req.request, HTTP_QUERY_VERSION, buffer, &sz, NULL);
     ok(r, "HttpQueryInfo failed\n");
     ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
 
     sz = sizeof buffer;
-    r = HttpQueryInfoA(hr, HTTP_QUERY_SERVER, buffer, &sz, NULL);
+    r = HttpQueryInfoA(req.request, HTTP_QUERY_SERVER, buffer, &sz, NULL);
     ok(r, "HttpQueryInfo failed\n");
     ok(!strcmp(buffer, "winetest"), "http server wrong\n");
 
     sz = sizeof buffer;
-    r = HttpQueryInfoA(hr, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
+    r = HttpQueryInfoA(req.request, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
     ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
     ok(r == FALSE, "HttpQueryInfo failed\n");
 
 out:
-    InternetCloseHandle(hr);
-    InternetCloseHandle(hc);
-    InternetCloseHandle(hi);
+    close_request(&req);
 }
 
 static void test_proxy_direct(int port)
@@ -2649,6 +2911,25 @@ static void test_proxy_direct(int port)
     ok(r, "HttpQueryInfo failed\n");
     ok(!strcmp(buffer, "200"), "proxy code wrong\n");
 
+    InternetCloseHandle(hr);
+    InternetCloseHandle(hc);
+    InternetCloseHandle(hi);
+
+    sprintf(buffer, "localhost:%d\n", port);
+    hi = InternetOpenA("winetest", INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
+    ok(hi != NULL, "InternetOpen failed\n");
+
+    hc = InternetConnectA(hi, "test.winehq.org", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
+    ok(hc != NULL, "InternetConnect failed\n");
+
+    hr = HttpOpenRequestA(hc, "POST", "/test2", NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0);
+    ok(hr != NULL, "HttpOpenRequest failed\n");
+
+    r = HttpSendRequestA(hr, NULL, 0, (char *)"data", sizeof("data"));
+    ok(r, "HttpSendRequest failed %u\n", GetLastError());
+
+    test_status_code(hr, 407);
+
 done:
     InternetCloseHandle(hr);
     InternetCloseHandle(hc);
@@ -2659,10 +2940,10 @@ static void test_header_handling_order(int port)
 {
     static const char authorization[] = "Authorization: Basic dXNlcjpwd2Q=";
     static const char connection[]    = "Connection: Close";
-
     static const char *types[2] = { "*", NULL };
+    char data[32];
     HINTERNET session, connect, request;
-    DWORD size, status;
+    DWORD size, status, data_len;
     BOOL ret;
 
     session = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
@@ -2698,6 +2979,10 @@ static void test_header_handling_order(int port)
     ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
 
     InternetCloseHandle(request);
+    InternetCloseHandle(connect);
+
+    connect = InternetConnectA(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
+    ok(connect != NULL, "InternetConnect failed\n");
 
     request = HttpOpenRequestA(connect, "POST", "/test7", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
     ok(request != NULL, "HttpOpenRequest failed\n");
@@ -2712,7 +2997,30 @@ static void test_header_handling_order(int port)
     size = sizeof(status);
     ret = HttpQueryInfoA( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
     ok(ret, "HttpQueryInfo failed\n");
-    ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
+    ok(status == 200, "got status %u, expected 200\n", status);
+
+    InternetCloseHandle(request);
+    InternetCloseHandle(connect);
+
+    connect = InternetConnectA(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
+    ok(connect != NULL, "InternetConnect failed\n");
+
+    request = HttpOpenRequestA(connect, "POST", "/test7b", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
+    ok(request != NULL, "HttpOpenRequest failed\n");
+
+    ret = HttpAddRequestHeadersA(request, "Content-Length: 100\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
+    ok(ret, "HttpAddRequestHeaders failed\n");
+
+    data_len = sizeof(data);
+    memset(data, 'a', sizeof(data));
+    ret = HttpSendRequestA(request, NULL, 0, data, data_len);
+    ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
+
+    status = 0;
+    size = sizeof(status);
+    ret = HttpQueryInfoA( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
+    ok(ret, "HttpQueryInfo failed\n");
+    ok(status == 200, "got status %u, expected 200\n", status);
 
     InternetCloseHandle(request);
     InternetCloseHandle(connect);
@@ -2773,48 +3081,272 @@ static void test_connection_header(int port)
     InternetCloseHandle(ses);
 }
 
-static void test_http1_1(int port)
+static void test_header_override(int port)
 {
+    char buffer[128], host_header_override[30], full_url[128];
     HINTERNET ses, con, req;
+    DWORD size, count, err;
     BOOL ret;
 
+    sprintf(host_header_override, "Host: test.local:%d\r\n", port);
+    sprintf(full_url, "http://localhost:%d/test_host_override", port);
+
     ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
     ok(ses != NULL, "InternetOpen failed\n");
 
     con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
     ok(con != NULL, "InternetConnect failed\n");
 
-    req = HttpOpenRequestA(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
+    req = HttpOpenRequestA(con, NULL, "/test_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
     ok(req != NULL, "HttpOpenRequest failed\n");
 
-    ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
-    if (ret)
-    {
-        InternetCloseHandle(req);
+    size = sizeof(buffer) - 1;
+    count = 0;
+    memset(buffer, 0, sizeof(buffer));
+    ret = HttpQueryInfoA(req, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, &count);
+    err = GetLastError();
+    ok(!ret, "HttpQueryInfo succeeded\n");
+    ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "Expected error ERROR_HTTP_HEADER_NOT_FOUND, got %d\n", err);
 
-        req = HttpOpenRequestA(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
-        ok(req != NULL, "HttpOpenRequest failed\n");
+    size = sizeof(buffer) - 1;
+    memset(buffer, 0, sizeof(buffer));
+    ret = InternetQueryOptionA(req, INTERNET_OPTION_URL, buffer, &size);
+    ok(ret, "InternetQueryOption failed\n");
+    ok(!strcmp(full_url, buffer), "Expected %s, got %s\n", full_url, buffer);
 
-        ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
-        ok(ret, "HttpSendRequest failed\n");
-    }
+    ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_COALESCE);
+    ok(ret, "HttpAddRequestHeaders failed\n");
 
-    InternetCloseHandle(req);
-    InternetCloseHandle(con);
-    InternetCloseHandle(ses);
-}
+    size = sizeof(buffer) - 1;
+    count = 0;
+    memset(buffer, 0, sizeof(buffer));
+    ret = HttpQueryInfoA(req, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, &count);
+    ok(ret, "HttpQueryInfo failed\n");
 
-static void test_connection_closing(int port)
-{
-    HINTERNET session, connection, req;
-    DWORD res;
+    size = sizeof(buffer) - 1;
+    memset(buffer, 0, sizeof(buffer));
+    ret = InternetQueryOptionA(req, INTERNET_OPTION_URL, buffer, &size);
+    ok(ret, "InternetQueryOption failed\n");
+    ok(!strcmp(full_url, buffer), "Expected %s, got %s\n", full_url, buffer);
 
-    hCompleteEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
+    ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequest failed\n");
 
-    session = InternetOpenA("", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
-    ok(session != NULL,"InternetOpen failed with error %u\n", GetLastError());
+    test_status_code(req, 200);
 
-    pInternetSetStatusCallbackA(session, callback);
+    InternetCloseHandle(req);
+    req = HttpOpenRequestA(con, NULL, "/test_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
+    ok(req != NULL, "HttpOpenRequest failed\n");
+
+    ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_COALESCE);
+    ok(ret, "HttpAddRequestHeaders failed\n");
+
+    ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_COALESCE);
+    ok(ret, "HttpAddRequestHeaders failed\n");
+
+    ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequest failed\n");
+
+    test_status_code(req, 400);
+
+    InternetCloseHandle(req);
+    req = HttpOpenRequestA(con, NULL, "/test_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
+    ok(req != NULL, "HttpOpenRequest failed\n");
+
+    ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_ADD);
+    ok(ret, "HttpAddRequestHeaders failed\n");
+
+    ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequest failed\n");
+
+    test_status_code(req, 200);
+
+    InternetCloseHandle(req);
+    req = HttpOpenRequestA(con, NULL, "/test_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
+    ok(req != NULL, "HttpOpenRequest failed\n");
+
+    ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_REPLACE);
+    if(ret) { /* win10 returns success */
+        trace("replacing host header is supported.\n");
+
+        ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
+        ok(ret, "HttpSendRequest failed\n");
+
+        test_status_code(req, 200);
+    }else {
+        trace("replacing host header is not supported.\n");
+
+        err = GetLastError();
+        ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "Expected error ERROR_HTTP_HEADER_NOT_FOUND, got %d\n", err);
+
+        ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
+        ok(ret, "HttpSendRequest failed\n");
+
+        test_status_code(req, 400);
+    }
+
+    InternetCloseHandle(req);
+    InternetSetCookieA("http://localhost", "cookie", "biscuit");
+    req = HttpOpenRequestA(con, NULL, "/testC", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
+    ok(req != NULL, "HttpOpenRequest failed\n");
+
+    ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_ADD);
+    ok(ret, "HttpAddRequestHeaders failed\n");
+
+    ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequest failed\n");
+
+    test_status_code(req, 200);
+
+    InternetCloseHandle(req);
+    req = HttpOpenRequestA(con, NULL, "/test_cookie_set_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
+    ok(req != NULL, "HttpOpenRequest failed\n");
+
+    ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_ADD);
+    ok(ret, "HttpAddRequestHeaders failed\n");
+
+    ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequest failed\n");
+
+    test_status_code(req, 200);
+
+    InternetCloseHandle(req);
+    req = HttpOpenRequestA(con, NULL, "/test_cookie_check_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
+    ok(req != NULL, "HttpOpenRequest failed\n");
+
+    ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_ADD);
+    ok(ret, "HttpAddRequestHeaders failed\n");
+
+    ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequest failed\n");
+
+    test_status_code(req, 200);
+
+    InternetCloseHandle(req);
+    req = HttpOpenRequestA(con, NULL, "/test_cookie_check_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
+    ok(req != NULL, "HttpOpenRequest failed\n");
+
+    ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequest failed\n");
+
+    test_status_code(req, 200);
+
+    InternetCloseHandle(req);
+    InternetSetCookieA("http://test.local", "foo", "bar");
+    req = HttpOpenRequestA(con, NULL, "/test_cookie_check_different_host", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
+    ok(req != NULL, "HttpOpenRequest failed\n");
+
+    ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequest failed\n");
+
+    test_status_code(req, 200);
+
+    InternetCloseHandle(req);
+    req = HttpOpenRequestA(con, NULL, "/test_cookie_check_different_host", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
+    ok(req != NULL, "HttpOpenRequest failed\n");
+
+    ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_ADD);
+    ok(ret, "HttpAddRequestHeaders failed\n");
+
+    ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequest failed\n");
+
+    test_status_code(req, 200);
+
+    InternetCloseHandle(req);
+    InternetCloseHandle(con);
+    InternetCloseHandle(ses);
+
+    ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
+    ok(ses != NULL, "InternetOpenA failed\n");
+
+    con = InternetConnectA(ses, "localhost", port, "test1", "pass", INTERNET_SERVICE_HTTP, 0, 0);
+    ok(con != NULL, "InternetConnectA failed %u\n", GetLastError());
+
+    req = HttpOpenRequestA( con, "HEAD", "/test_auth_host1", NULL, NULL, NULL, 0, 0);
+    ok(req != NULL, "HttpOpenRequestA failed %u\n", GetLastError());
+
+    ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequestA failed %u\n", GetLastError());
+
+    test_status_code(req, 200);
+
+    InternetCloseHandle(req);
+    InternetCloseHandle(con);
+    InternetCloseHandle(ses);
+
+    ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
+    ok(ses != NULL, "InternetOpenA failed\n");
+
+    con = InternetConnectA( ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
+    ok(con != NULL, "InternetConnectA failed %u\n", GetLastError());
+
+    req = HttpOpenRequestA(con, "HEAD", "/test_auth_host1", NULL, NULL, NULL, 0, 0);
+    ok(req != NULL, "HttpOpenRequestA failed %u\n", GetLastError());
+
+    ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_ADD);
+    ok(ret, "HttpAddRequestHeaders failed\n");
+
+    ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
+    ok( ret, "HttpSendRequestA failed %u\n", GetLastError() );
+
+    test_status_code(req, 200);
+
+    InternetCloseHandle(req);
+    InternetCloseHandle(con);
+    InternetCloseHandle(ses);
+
+    ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
+    ok(ses != NULL, "InternetOpenA failed\n");
+
+    con = InternetConnectA(ses, "localhost", port, "test1", "pass2", INTERNET_SERVICE_HTTP, 0, 0);
+    ok(con != NULL, "InternetConnectA failed %u\n", GetLastError());
+
+    req = HttpOpenRequestA(con, "HEAD", "/test_auth_host2", NULL, NULL, NULL, 0, 0);
+    ok(req != NULL, "HttpOpenRequestA failed %u\n", GetLastError());
+
+    ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_ADD);
+    ok(ret, "HttpAddRequestHeaders failed\n");
+
+    ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequestA failed %u\n", GetLastError());
+
+    test_status_code(req, 200);
+
+    InternetCloseHandle(req);
+    InternetCloseHandle(con);
+    InternetCloseHandle(ses);
+
+    ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
+    ok(ses != NULL, "InternetOpenA failed\n");
+
+    con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
+    ok(con != NULL, "InternetConnectA failed %u\n", GetLastError());
+
+    req = HttpOpenRequestA(con, "HEAD", "/test_auth_host2", NULL, NULL, NULL, 0, 0);
+    ok(req != NULL, "HttpOpenRequestA failed %u\n", GetLastError());
+
+    ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequestA failed %u\n", GetLastError());
+
+    test_status_code(req, 200);
+
+    InternetCloseHandle(req);
+    InternetCloseHandle(con);
+    InternetCloseHandle(ses);
+}
+
+static void test_connection_closing(int port)
+{
+    HINTERNET session, connection, req;
+    DWORD res;
+
+    hCompleteEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
+
+    session = InternetOpenA("", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
+    ok(session != NULL,"InternetOpen failed with error %u\n", GetLastError());
+
+    pInternetSetStatusCallbackA(session, callback);
 
     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
     connection = InternetConnectA(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
@@ -3019,7 +3551,7 @@ static void test_no_content(int port)
 
     hCompleteEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
 
-    session = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
+    session = InternetOpenA("", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
     ok(session != NULL,"InternetOpen failed with error %u\n", GetLastError());
 
     pInternetSetStatusCallbackA(session, callback);
@@ -3084,7 +3616,7 @@ static void test_conn_close(int port)
     hCompleteEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
     conn_close_event = CreateEventW(NULL, FALSE, FALSE, NULL);
 
-    session = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
+    session = InternetOpenA("", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
     ok(session != NULL,"InternetOpen failed with error %u\n", GetLastError());
 
     pInternetSetStatusCallbackA(session, callback);
@@ -3134,11 +3666,17 @@ static void test_conn_close(int port)
     res = InternetReadFile(req, buf, avail, &size);
     ok(res, "InternetReadFile failed: %u\n", GetLastError());
 
+    /* IE11 calls those in InternetQueryDataAvailable call. */
+    SET_OPTIONAL(INTERNET_STATUS_RECEIVING_RESPONSE);
+    SET_OPTIONAL(INTERNET_STATUS_RESPONSE_RECEIVED);
+
     res = InternetQueryDataAvailable(req, &avail, 0, 0);
     ok(!res && (GetLastError() == ERROR_IO_PENDING),
        "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
     ok(!avail, "avail = %u, expected 0\n", avail);
 
+    CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
+
     SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
     SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
     SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
@@ -3149,6 +3687,7 @@ static void test_conn_close(int port)
     ok(WaitForSingleObject(hCompleteEvent, 5000) == WAIT_OBJECT_0, "Wait timed out\n");
 #endif /* ROSTESTS_73_FIXED */
     ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
+    CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
     CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
     CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
     CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
@@ -3385,7 +3924,7 @@ static void test_cookie_header(int port)
     HINTERNET ses, con, req;
     DWORD size, error;
     BOOL ret;
-    char buffer[64];
+    char buffer[256];
 
     ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
     ok(ses != NULL, "InternetOpen failed\n");
@@ -3413,7 +3952,7 @@ static void test_cookie_header(int port)
     size = sizeof(buffer);
     ret = HttpQueryInfoA(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
     ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
-    ok(!strcmp(buffer, "cookie=not biscuit"), "got '%s' expected \'cookie=not biscuit\'\n", buffer);
+    ok(!!strstr(buffer, "cookie=not biscuit"), "got '%s' expected \'cookie=not biscuit\'\n", buffer);
 
     ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
@@ -3424,9 +3963,61 @@ static void test_cookie_header(int port)
     size = sizeof(buffer);
     ret = HttpQueryInfoA(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
     ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
-    ok(!strcmp(buffer, "cookie=biscuit"), "got '%s' expected \'cookie=biscuit\'\n", buffer);
+    ok(!strstr(buffer, "cookie=not biscuit"), "'%s' should not contain \'cookie=not biscuit\'\n", buffer);
+    ok(!!strstr(buffer, "cookie=biscuit"), "'%s' should contain \'cookie=biscuit\'\n", buffer);
+
+    InternetCloseHandle(req);
+
+    InternetSetCookieA("http://localhost/testCCCC", "subcookie", "data");
+
+    req = HttpOpenRequestA(con, NULL, "/test_cookie_path1", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
+    ok(req != NULL, "HttpOpenRequest failed\n");
+
+    ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequest failed\n");
+
+    test_status_code(req, 200);
+    InternetCloseHandle(req);
+
+    req = HttpOpenRequestA(con, NULL, "/test_cookie_path1/abc", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
+    ok(req != NULL, "HttpOpenRequest failed\n");
+
+    ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequest failed\n");
+
+    test_status_code(req, 200);
+    InternetCloseHandle(req);
+
+    req = HttpOpenRequestA(con, NULL, "/test_cookie_set_path", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
+    ok(req != NULL, "HttpOpenRequest failed\n");
+
+    ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequest failed\n");
+
+    test_status_code(req, 200);
+    InternetCloseHandle(req);
+
+    req = HttpOpenRequestA(con, NULL, "/test_cookie_path2", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
+    ok(req != NULL, "HttpOpenRequest failed\n");
+
+    ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequest failed\n");
+
+    test_status_code(req, 400);
+    InternetCloseHandle(req);
+
+    req = HttpOpenRequestA(con, NULL, "/test_cookie_merge", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
+    ok(req != NULL, "HttpOpenRequest failed\n");
 
+    ret = HttpAddRequestHeadersA(req, "Cookie: manual_cookie=test\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD);
+    ok(ret, "HttpAddRequestHeaders failed: %u\n", GetLastError());
+
+    ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequest failed\n");
+
+    test_status_code(req, 200);
     InternetCloseHandle(req);
+
     InternetCloseHandle(con);
     InternetCloseHandle(ses);
 }
@@ -3458,239 +4049,225 @@ static void test_basic_authentication(int port)
 
 static void test_premature_disconnect(int port)
 {
-    HINTERNET session, connect, request;
+    test_request_t req;
     DWORD err;
     BOOL ret;
 
-    session = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
-    ok(session != NULL, "InternetOpen failed\n");
-
-    connect = InternetConnectA(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
-    ok(connect != NULL, "InternetConnect failed\n");
-
-    request = HttpOpenRequestA(connect, NULL, "/premature_disconnect", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
-    ok(request != NULL, "HttpOpenRequest failed\n");
+    open_simple_request(&req, "localhost", port, NULL, "/premature_disconnect");
 
     SetLastError(0xdeadbeef);
-    ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
+    ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
     err = GetLastError();
     todo_wine ok(!ret, "HttpSendRequest succeeded\n");
     todo_wine ok(err == ERROR_HTTP_INVALID_SERVER_RESPONSE, "got %u\n", err);
 
-    InternetCloseHandle(request);
-    InternetCloseHandle(connect);
-    InternetCloseHandle(session);
+    close_request(&req);
 }
 
 static void test_invalid_response_headers(int port)
 {
-    HINTERNET session, connect, request;
+    test_request_t req;
     DWORD size;
     BOOL ret;
     char buffer[256];
 
-    session = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
-    ok(session != NULL, "InternetOpen failed\n");
-
-    connect = InternetConnectA(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
-    ok(connect != NULL, "InternetConnect failed\n");
-
-    request = HttpOpenRequestA(connect, NULL, "/testE", NULL, NULL, NULL, 0, 0);
-    ok(request != NULL, "HttpOpenRequest failed\n");
+    open_simple_request(&req, "localhost", port, NULL, "/testE");
 
-    ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
+    ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
     ok(ret, "HttpSendRequest failed %u\n", GetLastError());
 
-    test_status_code(request, 401);
-    test_request_flags(request, 0);
+    test_status_code(req.request, 401);
+    test_request_flags(req.request, 0);
 
     buffer[0] = 0;
     size = sizeof(buffer);
-    ret = HttpQueryInfoA( request, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
+    ret = HttpQueryInfoA(req.request, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
     ok(ret, "HttpQueryInfo failed\n");
     ok(!strcmp(buffer, "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"),
        "headers wrong \"%s\"\n", buffer);
 
     buffer[0] = 0;
     size = sizeof(buffer);
-    ret = HttpQueryInfoA( request, HTTP_QUERY_SERVER, buffer, &size, NULL);
+    ret = HttpQueryInfoA(req.request, HTTP_QUERY_SERVER, buffer, &size, NULL);
     ok(ret, "HttpQueryInfo failed\n");
     ok(!strcmp(buffer, "winetest"), "server wrong \"%s\"\n", buffer);
 
-    InternetCloseHandle(request);
-    InternetCloseHandle(connect);
-    InternetCloseHandle(session);
+    close_request(&req);
 }
 
 static void test_response_without_headers(int port)
 {
-    HINTERNET hi, hc, hr;
+    test_request_t req;
     DWORD r, count, size;
     char buffer[1024];
 
-    SetLastError(0xdeadbeef);
-    hi = InternetOpenA(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
-    ok(hi != NULL, "open failed %u\n", GetLastError());
-
-    SetLastError(0xdeadbeef);
-    hc = InternetConnectA(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
-    ok(hc != NULL, "connect failed %u\n", GetLastError());
-
-    SetLastError(0xdeadbeef);
-    hr = HttpOpenRequestA(hc, NULL, "/testG", NULL, NULL, NULL, 0, 0);
-    ok(hr != NULL, "HttpOpenRequest failed %u\n", GetLastError());
+    open_simple_request(&req, "localhost", port, NULL, "/testG");
 
-    test_request_flags(hr, INTERNET_REQFLAG_NO_HEADERS);
+    test_request_flags(req.request, INTERNET_REQFLAG_NO_HEADERS);
 
-    SetLastError(0xdeadbeef);
-    r = HttpSendRequestA(hr, NULL, 0, NULL, 0);
+    r = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
     ok(r, "HttpSendRequest failed %u\n", GetLastError());
 
-    test_request_flags_todo(hr, INTERNET_REQFLAG_NO_HEADERS);
+    test_request_flags_todo(req.request, INTERNET_REQFLAG_NO_HEADERS);
 
     count = 0;
     memset(buffer, 0, sizeof buffer);
-    SetLastError(0xdeadbeef);
-    r = InternetReadFile(hr, buffer, sizeof buffer, &count);
+    r = InternetReadFile(req.request, buffer, sizeof buffer, &count);
     ok(r, "InternetReadFile failed %u\n", GetLastError());
     todo_wine ok(count == sizeof page1 - 1, "count was wrong\n");
     todo_wine ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
 
-    test_status_code(hr, 200);
-    test_request_flags_todo(hr, INTERNET_REQFLAG_NO_HEADERS);
+    test_status_code(req.request, 200);
+    test_request_flags_todo(req.request, INTERNET_REQFLAG_NO_HEADERS);
 
     buffer[0] = 0;
     size = sizeof(buffer);
-    SetLastError(0xdeadbeef);
-    r = HttpQueryInfoA(hr, HTTP_QUERY_STATUS_TEXT, buffer, &size, NULL );
+    r = HttpQueryInfoA(req.request, HTTP_QUERY_STATUS_TEXT, buffer, &size, NULL );
     ok(r, "HttpQueryInfo failed %u\n", GetLastError());
     ok(!strcmp(buffer, "OK"), "expected OK got: \"%s\"\n", buffer);
 
     buffer[0] = 0;
     size = sizeof(buffer);
-    SetLastError(0xdeadbeef);
-    r = HttpQueryInfoA(hr, HTTP_QUERY_VERSION, buffer, &size, NULL);
+    r = HttpQueryInfoA(req.request, HTTP_QUERY_VERSION, buffer, &size, NULL);
     ok(r, "HttpQueryInfo failed %u\n", GetLastError());
     ok(!strcmp(buffer, "HTTP/1.0"), "expected HTTP/1.0 got: \"%s\"\n", buffer);
 
     buffer[0] = 0;
     size = sizeof(buffer);
-    SetLastError(0xdeadbeef);
-    r = HttpQueryInfoA(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
+    r = HttpQueryInfoA(req.request, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
     ok(r, "HttpQueryInfo failed %u\n", GetLastError());
     ok(!strcmp(buffer, "HTTP/1.0 200 OK"), "raw headers wrong: \"%s\"\n", buffer);
 
-    InternetCloseHandle(hr);
-    InternetCloseHandle(hc);
-    InternetCloseHandle(hi);
+    close_request(&req);
 }
 
-static void test_HttpQueryInfo(int port)
+static void test_head_request(int port)
 {
-    HINTERNET hi, hc, hr;
-    DWORD size, index, error;
-    char buffer[1024];
+    DWORD len, content_length;
+    test_request_t req;
+    BYTE buf[100];
     BOOL ret;
 
-    hi = InternetOpenA(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
-    ok(hi != NULL, "InternetOpen failed\n");
+    open_simple_request(&req, "localhost", port, "HEAD", "/test_head");
 
-    hc = InternetConnectA(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
-    ok(hc != NULL, "InternetConnect failed\n");
+    ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
 
-    hr = HttpOpenRequestA(hc, NULL, "/testD", NULL, NULL, NULL, 0, 0);
-    ok(hr != NULL, "HttpOpenRequest failed\n");
+    len = sizeof(content_length);
+    content_length = -1;
+    ret = HttpQueryInfoA(req.request, HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH, &content_length, &len, 0);
+    ok(ret, "HttpQueryInfo dailed: %u\n", GetLastError());
+    ok(len == sizeof(DWORD), "len = %u\n", len);
+    ok(content_length == 100, "content_length = %u\n", content_length);
+
+    len = -1;
+    ret = InternetReadFile(req.request, buf, sizeof(buf), &len);
+    ok(ret, "InternetReadFile failed: %u\n", GetLastError());
+
+    len = -1;
+    ret = InternetReadFile(req.request, buf, sizeof(buf), &len);
+    ok(ret, "InternetReadFile failed: %u\n", GetLastError());
+
+    close_request(&req);
+}
+
+static void test_HttpQueryInfo(int port)
+{
+    test_request_t req;
+    DWORD size, index, error;
+    char buffer[1024];
+    BOOL ret;
+
+    open_simple_request(&req, "localhost", port, NULL, "/testD");
 
     size = sizeof(buffer);
-    ret = HttpQueryInfoA(hr, HTTP_QUERY_STATUS_TEXT, buffer, &size, &index);
+    ret = HttpQueryInfoA(req.request, HTTP_QUERY_STATUS_TEXT, buffer, &size, &index);
     error = GetLastError();
     ok(!ret || broken(ret), "HttpQueryInfo succeeded\n");
     if (!ret) ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "got %u expected ERROR_HTTP_HEADER_NOT_FOUND\n", error);
 
-    ret = HttpSendRequestA(hr, NULL, 0, NULL, 0);
+    ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
     ok(ret, "HttpSendRequest failed\n");
 
     index = 0;
     size = sizeof(buffer);
-    ret = HttpQueryInfoA(hr, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, &index);
+    ret = HttpQueryInfoA(req.request, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, &index);
     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
     ok(index == 1, "expected 1 got %u\n", index);
 
     index = 0;
     size = sizeof(buffer);
-    ret = HttpQueryInfoA(hr, HTTP_QUERY_DATE | HTTP_QUERY_FLAG_SYSTEMTIME, buffer, &size, &index);
+    ret = HttpQueryInfoA(req.request, HTTP_QUERY_DATE | HTTP_QUERY_FLAG_SYSTEMTIME, buffer, &size, &index);
     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
     ok(index == 1, "expected 1 got %u\n", index);
 
     index = 0;
     size = sizeof(buffer);
-    ret = HttpQueryInfoA(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
+    ret = HttpQueryInfoA(req.request, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
     ok(index == 0, "expected 0 got %u\n", index);
 
     size = sizeof(buffer);
-    ret = HttpQueryInfoA(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
+    ret = HttpQueryInfoA(req.request, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
     ok(index == 0, "expected 0 got %u\n", index);
 
     index = 0xdeadbeef; /* invalid start index */
     size = sizeof(buffer);
-    ret = HttpQueryInfoA(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
+    ret = HttpQueryInfoA(req.request, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
     todo_wine ok(!ret, "HttpQueryInfo should have failed\n");
     todo_wine ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
        "Expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", GetLastError());
 
     index = 0;
     size = sizeof(buffer);
-    ret = HttpQueryInfoA(hr, HTTP_QUERY_RAW_HEADERS_CRLF, buffer, &size, &index);
+    ret = HttpQueryInfoA(req.request, HTTP_QUERY_RAW_HEADERS_CRLF, buffer, &size, &index);
     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
     ok(index == 0, "expected 0 got %u\n", index);
 
     size = sizeof(buffer);
-    ret = HttpQueryInfoA(hr, HTTP_QUERY_STATUS_TEXT, buffer, &size, &index);
+    ret = HttpQueryInfoA(req.request, HTTP_QUERY_STATUS_TEXT, buffer, &size, &index);
     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
     ok(index == 0, "expected 0 got %u\n", index);
 
     size = sizeof(buffer);
-    ret = HttpQueryInfoA(hr, HTTP_QUERY_VERSION, buffer, &size, &index);
+    ret = HttpQueryInfoA(req.request, HTTP_QUERY_VERSION, buffer, &size, &index);
     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
     ok(index == 0, "expected 0 got %u\n", index);
 
-    test_status_code(hr, 200);
+    test_status_code(req.request, 200);
 
     index = 0xdeadbeef;
     size = sizeof(buffer);
-    ret = HttpQueryInfoA(hr, HTTP_QUERY_FORWARDED, buffer, &size, &index);
+    ret = HttpQueryInfoA(req.request, HTTP_QUERY_FORWARDED, buffer, &size, &index);
     ok(!ret, "HttpQueryInfo succeeded\n");
     ok(index == 0xdeadbeef, "expected 0xdeadbeef got %u\n", index);
 
     index = 0;
     size = sizeof(buffer);
-    ret = HttpQueryInfoA(hr, HTTP_QUERY_SERVER, buffer, &size, &index);
+    ret = HttpQueryInfoA(req.request, HTTP_QUERY_SERVER, buffer, &size, &index);
     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
     ok(index == 1, "expected 1 got %u\n", index);
 
     index = 0;
     size = sizeof(buffer);
     strcpy(buffer, "Server");
-    ret = HttpQueryInfoA(hr, HTTP_QUERY_CUSTOM, buffer, &size, &index);
+    ret = HttpQueryInfoA(req.request, HTTP_QUERY_CUSTOM, buffer, &size, &index);
     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
     ok(index == 1, "expected 1 got %u\n", index);
 
     index = 0;
     size = sizeof(buffer);
-    ret = HttpQueryInfoA(hr, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
+    ret = HttpQueryInfoA(req.request, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
     ok(index == 1, "expected 1 got %u\n", index);
 
     size = sizeof(buffer);
-    ret = HttpQueryInfoA(hr, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
+    ret = HttpQueryInfoA(req.request, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
     ok(index == 2, "expected 2 got %u\n", index);
 
-    InternetCloseHandle(hr);
-    InternetCloseHandle(hc);
-    InternetCloseHandle(hi);
+    close_request(&req);
 }
 
 static void test_options(int port)
@@ -3767,191 +4344,793 @@ static void test_options(int port)
     ok(ret, "InternetQueryOption failed %u\n", GetLastError());
     ok(ctx == 0, "expected 0 got %lu\n", ctx);
 
-    ctx = 2;
-    ret = InternetSetOptionA(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
-    ok(ret, "InternetSetOption failed %u\n", GetLastError());
+    ctx = 2;
+    ret = InternetSetOptionA(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
+    ok(ret, "InternetSetOption failed %u\n", GetLastError());
+
+    ctx = 0xdeadbeef;
+    size = sizeof(ctx);
+    ret = InternetQueryOptionA(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
+    ok(ret, "InternetQueryOption failed %u\n", GetLastError());
+    ok(ctx == 2, "expected 2 got %lu\n", ctx);
+
+    req = HttpOpenRequestA(con, NULL, "/test1", NULL, NULL, NULL, 0, 0);
+    ok(req != NULL, "HttpOpenRequest failed\n");
+
+    ctx = 0xdeadbeef;
+    size = sizeof(ctx);
+    ret = InternetQueryOptionA(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
+    ok(ret, "InternetQueryOption failed %u\n", GetLastError());
+    ok(ctx == 0, "expected 0 got %lu\n", ctx);
+
+    ctx = 3;
+    ret = InternetSetOptionA(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
+    ok(ret, "InternetSetOption failed %u\n", GetLastError());
+
+    ctx = 0xdeadbeef;
+    size = sizeof(ctx);
+    ret = InternetQueryOptionA(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
+    ok(ret, "InternetQueryOption failed %u\n", GetLastError());
+    ok(ctx == 3, "expected 3 got %lu\n", ctx);
+
+    size = sizeof(idsi);
+    ret = InternetQueryOptionA(req, INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO, &idsi, &size);
+    ok(ret, "InternetQueryOption failed %u\n", GetLastError());
+
+    size = 0;
+    SetLastError(0xdeadbeef);
+    ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT, NULL, &size);
+    error = GetLastError();
+    ok(!ret, "InternetQueryOption succeeded\n");
+    ok(error == ERROR_INTERNET_INVALID_OPERATION, "expected ERROR_INTERNET_INVALID_OPERATION, got %u\n", error);
+
+    /* INTERNET_OPTION_PROXY */
+    SetLastError(0xdeadbeef);
+    ret = InternetQueryOptionA(ses, INTERNET_OPTION_PROXY, NULL, NULL);
+    error = GetLastError();
+    ok(!ret, "InternetQueryOption succeeded\n");
+    ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
+
+    SetLastError(0xdeadbeef);
+    ret = InternetQueryOptionA(ses, INTERNET_OPTION_PROXY, &ctx, NULL);
+    error = GetLastError();
+    ok(!ret, "InternetQueryOption succeeded\n");
+    ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
+
+    size = 0;
+    SetLastError(0xdeadbeef);
+    ret = InternetQueryOptionA(ses, INTERNET_OPTION_PROXY, NULL, &size);
+    error = GetLastError();
+    ok(!ret, "InternetQueryOption succeeded\n");
+    ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
+    ok(size >= sizeof(INTERNET_PROXY_INFOA), "expected size to be greater or equal to the struct size\n");
+
+    InternetCloseHandle(req);
+    InternetCloseHandle(con);
+    InternetCloseHandle(ses);
+}
+
+typedef struct {
+    const char *response_text;
+    int status_code;
+    const char *status_text;
+    const char *raw_headers;
+} http_status_test_t;
+
+static const http_status_test_t http_status_tests[] = {
+    {
+        "HTTP/1.1 200 OK\r\n"
+        "Content-Length: 1\r\n"
+        "\r\nx",
+        200,
+        "OK"
+    },
+    {
+        "HTTP/1.1 404 Fail\r\n"
+        "Content-Length: 1\r\n"
+        "\r\nx",
+        404,
+        "Fail"
+    },
+    {
+        "HTTP/1.1 200\r\n"
+        "Content-Length: 1\r\n"
+        "\r\nx",
+        200,
+        ""
+    },
+    {
+        "HTTP/1.1 410 \r\n"
+        "Content-Length: 1\r\n"
+        "\r\nx",
+        410,
+        ""
+    }
+};
+
+static void test_http_status(int port)
+{
+    test_request_t req;
+    char buf[1000];
+    DWORD i, size;
+    BOOL res;
+
+    for(i=0; i < sizeof(http_status_tests)/sizeof(*http_status_tests); i++) {
+        send_buffer = http_status_tests[i].response_text;
+
+        open_simple_request(&req, "localhost", port, NULL, "/send_from_buffer");
+
+        res = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
+        ok(res, "HttpSendRequest failed\n");
+
+        test_status_code(req.request, http_status_tests[i].status_code);
+
+        size = sizeof(buf);
+        res = HttpQueryInfoA(req.request, HTTP_QUERY_STATUS_TEXT, buf, &size, NULL);
+        ok(res, "HttpQueryInfo failed: %u\n", GetLastError());
+        ok(!strcmp(buf, http_status_tests[i].status_text), "[%u] Unexpected status text \"%s\", expected \"%s\"\n",
+           i, buf, http_status_tests[i].status_text);
+
+        close_request(&req);
+    }
+}
+
+static void test_cache_control_verb(int port)
+{
+    HINTERNET session, connect, request;
+    BOOL ret;
+
+    session = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
+    ok(session != NULL, "InternetOpen failed\n");
+
+    connect = InternetConnectA(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
+    ok(connect != NULL, "InternetConnect failed\n");
+
+    request = HttpOpenRequestA(connect, "RPC_OUT_DATA", "/test_cache_control_verb", NULL, NULL, NULL,
+                              INTERNET_FLAG_NO_CACHE_WRITE, 0);
+    ok(request != NULL, "HttpOpenRequest failed\n");
+    ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequest failed %u\n", GetLastError());
+    test_status_code(request, 200);
+    InternetCloseHandle(request);
+
+    request = HttpOpenRequestA(connect, "POST", "/test_cache_control_verb", NULL, NULL, NULL,
+                              INTERNET_FLAG_NO_CACHE_WRITE, 0);
+    ok(request != NULL, "HttpOpenRequest failed\n");
+    ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequest failed %u\n", GetLastError());
+    test_status_code(request, 200);
+    InternetCloseHandle(request);
+
+    request = HttpOpenRequestA(connect, "HEAD", "/test_cache_control_verb", NULL, NULL, NULL,
+                              INTERNET_FLAG_NO_CACHE_WRITE, 0);
+    ok(request != NULL, "HttpOpenRequest failed\n");
+    ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequest failed %u\n", GetLastError());
+    test_status_code(request, 200);
+    InternetCloseHandle(request);
+
+    request = HttpOpenRequestA(connect, "GET", "/test_cache_control_verb", NULL, NULL, NULL,
+                              INTERNET_FLAG_NO_CACHE_WRITE, 0);
+    ok(request != NULL, "HttpOpenRequest failed\n");
+    ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequest failed %u\n", GetLastError());
+    test_status_code(request, 200);
+    InternetCloseHandle(request);
+
+    InternetCloseHandle(connect);
+    InternetCloseHandle(session);
+}
+
+static void test_request_content_length(int port)
+{
+    char data[] = {'t','e','s','t'};
+    test_request_t req;
+    BOOL ret;
+
+    hCompleteEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
+
+    open_simple_request(&req, "localhost", port, "POST", "/test_request_content_length");
+
+    ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequest failed %u\n", GetLastError());
+    test_status_code(req.request, 200);
+
+    SetEvent(hCompleteEvent);
+
+    ret = HttpSendRequestA(req.request, NULL, 0, data, sizeof(data));
+    ok(ret, "HttpSendRequest failed %u\n", GetLastError());
+    test_status_code(req.request, 200);
+
+    SetEvent(hCompleteEvent);
+
+    close_request(&req);
+    CloseHandle(hCompleteEvent);
+}
+
+static void test_accept_encoding(int port)
+{
+    HINTERNET ses, con, req;
+    char buf[1000];
+    BOOL ret;
+
+    ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
+    ok(ses != NULL, "InternetOpen failed\n");
+
+    con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
+    ok(con != NULL, "InternetConnect failed\n");
+
+    req = HttpOpenRequestA(con, "GET", "/echo_request", "HTTP/1.0", NULL, NULL, 0, 0);
+    ok(req != NULL, "HttpOpenRequest failed\n");
+
+    ret = HttpAddRequestHeadersA(req, "Accept-Encoding: gzip\r\n", ~0u, HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
+    ok(ret, "HttpAddRequestHeaders failed\n");
+
+    ret = HttpSendRequestA(req, NULL,  0, NULL, 0);
+    ok(ret, "HttpSendRequestA failed\n");
+
+    test_status_code(req, 200);
+    receive_simple_request(req, buf, sizeof(buf));
+    ok(strstr(buf, "Accept-Encoding: gzip") != NULL, "Accept-Encoding header not found in %s\n", buf);
+
+    InternetCloseHandle(req);
+
+    req = HttpOpenRequestA(con, "GET", "/echo_request", "HTTP/1.0", NULL, NULL, 0, 0);
+    ok(req != NULL, "HttpOpenRequest failed\n");
+
+    ret = HttpSendRequestA(req, "Accept-Encoding: gzip", ~0u, NULL, 0);
+    ok(ret, "HttpSendRequestA failed\n");
+
+    test_status_code(req, 200);
+    receive_simple_request(req, buf, sizeof(buf));
+    ok(strstr(buf, "Accept-Encoding: gzip") != NULL, "Accept-Encoding header not found in %s\n", buf);
+
+    InternetCloseHandle(req);
+    InternetCloseHandle(con);
+    InternetCloseHandle(ses);
+}
+
+static void test_basic_auth_credentials_reuse(int port)
+{
+    HINTERNET ses, con, req;
+    DWORD status, size;
+    BOOL ret;
+
+    ses = InternetOpenA( "winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0 );
+    ok( ses != NULL, "InternetOpenA failed\n" );
+
+    con = InternetConnectA( ses, "localhost", port, "user", "pwd",
+                            INTERNET_SERVICE_HTTP, 0, 0 );
+    ok( con != NULL, "InternetConnectA failed %u\n", GetLastError() );
+
+    req = HttpOpenRequestA( con, "HEAD", "/upload.txt", NULL, NULL, NULL, 0, 0 );
+    ok( req != NULL, "HttpOpenRequestA failed %u\n", GetLastError() );
+
+    ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
+    ok( ret, "HttpSendRequestA failed %u\n", GetLastError() );
+
+    status = 0xdeadbeef;
+    size = sizeof(status);
+    ret = HttpQueryInfoA( req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
+    ok( ret, "HttpQueryInfoA failed %u\n", GetLastError() );
+    ok( status == 200, "got %u\n", status );
+
+    InternetCloseHandle( req );
+    InternetCloseHandle( con );
+    InternetCloseHandle( ses );
+
+    ses = InternetOpenA( "winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0 );
+    ok( ses != NULL, "InternetOpenA failed\n" );
+
+    con = InternetConnectA( ses, "localhost", port, NULL, NULL,
+                            INTERNET_SERVICE_HTTP, 0, 0 );
+    ok( con != NULL, "InternetConnectA failed %u\n", GetLastError() );
+
+    req = HttpOpenRequestA( con, "PUT", "/upload2.txt", NULL, NULL, NULL, 0, 0 );
+    ok( req != NULL, "HttpOpenRequestA failed %u\n", GetLastError() );
+
+    ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
+    ok( ret, "HttpSendRequestA failed %u\n", GetLastError() );
+
+    status = 0xdeadbeef;
+    size = sizeof(status);
+    ret = HttpQueryInfoA( req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
+    ok( ret, "HttpQueryInfoA failed %u\n", GetLastError() );
+    ok( status == 200, "got %u\n", status );
+
+    InternetCloseHandle( req );
+    InternetCloseHandle( con );
+    InternetCloseHandle( ses );
+}
+
+static void test_async_read(int port)
+{
+    HINTERNET ses, con, req;
+    INTERNET_BUFFERSA ib;
+    char buffer[0x100];
+    DWORD pending_reads;
+    DWORD res, count, bytes;
+    BOOL ret;
+
+    hCompleteEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
+    conn_wait_event = CreateEventW(NULL, FALSE, FALSE, NULL);
+
+    /* test asynchronous InternetReadFileEx */
+    ses = InternetOpenA( "winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC );
+    ok( ses != NULL, "InternetOpenA failed\n" );
+    pInternetSetStatusCallbackA( ses, &callback );
+
+    SET_EXPECT( INTERNET_STATUS_HANDLE_CREATED );
+    con = InternetConnectA( ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0xdeadbeef );
+    ok( con != NULL, "InternetConnectA failed %u\n", GetLastError() );
+    CHECK_NOTIFIED( INTERNET_STATUS_HANDLE_CREATED );
+
+    SET_EXPECT( INTERNET_STATUS_HANDLE_CREATED );
+    req = HttpOpenRequestA( con, "GET", "/async_read", NULL, NULL, NULL, INTERNET_FLAG_RELOAD, 0xdeadbeef );
+    ok( req != NULL, "HttpOpenRequestA failed %u\n", GetLastError() );
+    CHECK_NOTIFIED( INTERNET_STATUS_HANDLE_CREATED );
+
+    SET_OPTIONAL( INTERNET_STATUS_COOKIE_SENT );
+    SET_OPTIONAL( INTERNET_STATUS_DETECTING_PROXY );
+    SET_EXPECT( INTERNET_STATUS_CONNECTING_TO_SERVER );
+    SET_EXPECT( INTERNET_STATUS_CONNECTED_TO_SERVER );
+    SET_EXPECT( INTERNET_STATUS_SENDING_REQUEST );
+    SET_EXPECT( INTERNET_STATUS_REQUEST_SENT );
+    SET_EXPECT( INTERNET_STATUS_RECEIVING_RESPONSE );
+    SET_EXPECT( INTERNET_STATUS_RESPONSE_RECEIVED );
+    SET_OPTIONAL( INTERNET_STATUS_CLOSING_CONNECTION );
+    SET_OPTIONAL( INTERNET_STATUS_CONNECTION_CLOSED );
+    SET_EXPECT( INTERNET_STATUS_REQUEST_COMPLETE );
+
+    SetLastError( 0xdeadbeef );
+    ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
+    ok( !ret, "HttpSendRequestA unexpectedly succeeded\n" );
+    ok( GetLastError() == ERROR_IO_PENDING, "expected ERROR_IO_PENDING, got %u\n", GetLastError() );
+    WaitForSingleObject( hCompleteEvent, INFINITE );
+    ok( req_error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %u\n", req_error );
+
+    CLEAR_NOTIFIED( INTERNET_STATUS_COOKIE_SENT );
+    CLEAR_NOTIFIED( INTERNET_STATUS_DETECTING_PROXY );
+    CHECK_NOTIFIED( INTERNET_STATUS_CONNECTING_TO_SERVER );
+    CHECK_NOTIFIED( INTERNET_STATUS_CONNECTED_TO_SERVER );
+    CHECK_NOTIFIED( INTERNET_STATUS_SENDING_REQUEST );
+    CHECK_NOTIFIED( INTERNET_STATUS_REQUEST_SENT );
+    CHECK_NOTIFIED( INTERNET_STATUS_RECEIVING_RESPONSE );
+    CHECK_NOTIFIED( INTERNET_STATUS_RESPONSE_RECEIVED );
+    CLEAR_NOTIFIED( INTERNET_STATUS_CLOSING_CONNECTION );
+    CLEAR_NOTIFIED( INTERNET_STATUS_CONNECTION_CLOSED );
+    CHECK_NOTIFIED( INTERNET_STATUS_REQUEST_COMPLETE );
+
+    pending_reads = 0;
+    memset( &ib, 0, sizeof(ib) );
+    memset( buffer, 0, sizeof(buffer) );
+    ib.dwStructSize = sizeof(ib);
+    for (count = 0; count < sizeof(buffer); count += ib.dwBufferLength)
+    {
+        ib.lpvBuffer = buffer + count;
+        ib.dwBufferLength = min(16, sizeof(buffer) - count);
+
+        SET_EXPECT( INTERNET_STATUS_RECEIVING_RESPONSE );
+        SET_EXPECT( INTERNET_STATUS_RESPONSE_RECEIVED );
+
+        ret = InternetReadFileExA( req, &ib, 0, 0xdeadbeef );
+        if (!count) /* the first part should arrive immediately */
+            ok( ret, "InternetReadFileExA failed %u\n", GetLastError() );
+        if (!ret)
+        {
+            ok( GetLastError() == ERROR_IO_PENDING, "expected ERROR_IO_PENDING, got %u\n", GetLastError() );
+            CHECK_NOTIFIED( INTERNET_STATUS_RECEIVING_RESPONSE );
+            SET_EXPECT( INTERNET_STATUS_REQUEST_COMPLETE );
+            if (!pending_reads++)
+            {
+                res = WaitForSingleObject( hCompleteEvent, 0 );
+                ok( res == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %u\n", res );
+                SetEvent( conn_wait_event );
+            }
+            res = WaitForSingleObject( hCompleteEvent, INFINITE );
+            ok( res == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", res );
+            ok( req_error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %u\n", req_error );
+            todo_wine_if( pending_reads > 1 )
+            ok( ib.dwBufferLength != 0, "expected ib.dwBufferLength != 0\n" );
+            CHECK_NOTIFIED( INTERNET_STATUS_RESPONSE_RECEIVED );
+            CHECK_NOTIFIED( INTERNET_STATUS_REQUEST_COMPLETE );
+        }
+
+        CLEAR_NOTIFIED( INTERNET_STATUS_RECEIVING_RESPONSE );
+        CLEAR_NOTIFIED( INTERNET_STATUS_RESPONSE_RECEIVED );
+        if (!ib.dwBufferLength) break;
+    }
+
+    todo_wine
+    ok( pending_reads == 1, "expected 1 pending read, got %u\n", pending_reads );
+    ok( !strcmp(buffer, page1), "unexpected buffer content\n" );
+    close_async_handle( ses, hCompleteEvent, 2 );
+    ResetEvent( conn_wait_event );
+
+    /* test asynchronous InternetReadFile */
+    ses = InternetOpenA( "winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC );
+    ok( ses != NULL, "InternetOpenA failed\n" );
+    pInternetSetStatusCallbackA( ses, &callback );
+
+    SET_EXPECT( INTERNET_STATUS_HANDLE_CREATED );
+    con = InternetConnectA( ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0xdeadbeef );
+    ok( con != NULL, "InternetConnectA failed %u\n", GetLastError() );
+    CHECK_NOTIFIED( INTERNET_STATUS_HANDLE_CREATED );
+
+    SET_EXPECT( INTERNET_STATUS_HANDLE_CREATED );
+    req = HttpOpenRequestA( con, "GET", "/async_read", NULL, NULL, NULL, INTERNET_FLAG_RELOAD, 0xdeadbeef );
+    ok( req != NULL, "HttpOpenRequestA failed %u\n", GetLastError() );
+    CHECK_NOTIFIED( INTERNET_STATUS_HANDLE_CREATED );
+
+    SET_OPTIONAL( INTERNET_STATUS_COOKIE_SENT );
+    SET_OPTIONAL( INTERNET_STATUS_DETECTING_PROXY );
+    SET_EXPECT( INTERNET_STATUS_CONNECTING_TO_SERVER );
+    SET_EXPECT( INTERNET_STATUS_CONNECTED_TO_SERVER );
+    SET_EXPECT( INTERNET_STATUS_SENDING_REQUEST );
+    SET_EXPECT( INTERNET_STATUS_REQUEST_SENT );
+    SET_EXPECT( INTERNET_STATUS_RECEIVING_RESPONSE );
+    SET_EXPECT( INTERNET_STATUS_RESPONSE_RECEIVED );
+    SET_OPTIONAL( INTERNET_STATUS_CLOSING_CONNECTION );
+    SET_OPTIONAL( INTERNET_STATUS_CONNECTION_CLOSED );
+    SET_EXPECT( INTERNET_STATUS_REQUEST_COMPLETE );
+
+    SetLastError( 0xdeadbeef );
+    ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
+    ok( !ret, "HttpSendRequestA unexpectedly succeeded\n" );
+    ok( GetLastError() == ERROR_IO_PENDING, "expected ERROR_IO_PENDING, got %u\n", GetLastError() );
+    WaitForSingleObject( hCompleteEvent, INFINITE );
+    ok( req_error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %u\n", req_error );
+
+    CLEAR_NOTIFIED( INTERNET_STATUS_COOKIE_SENT );
+    CLEAR_NOTIFIED( INTERNET_STATUS_DETECTING_PROXY );
+    CHECK_NOTIFIED( INTERNET_STATUS_CONNECTING_TO_SERVER );
+    CHECK_NOTIFIED( INTERNET_STATUS_CONNECTED_TO_SERVER );
+    CHECK_NOTIFIED( INTERNET_STATUS_SENDING_REQUEST );
+    CHECK_NOTIFIED( INTERNET_STATUS_REQUEST_SENT );
+    CHECK_NOTIFIED( INTERNET_STATUS_RECEIVING_RESPONSE );
+    CHECK_NOTIFIED( INTERNET_STATUS_RESPONSE_RECEIVED );
+    CLEAR_NOTIFIED( INTERNET_STATUS_CLOSING_CONNECTION );
+    CLEAR_NOTIFIED( INTERNET_STATUS_CONNECTION_CLOSED );
+    CHECK_NOTIFIED( INTERNET_STATUS_REQUEST_COMPLETE );
+
+    pending_reads = 0;
+    memset( buffer, 0, sizeof(buffer) );
+    for (count = 0; count < sizeof(buffer); count += bytes)
+    {
+        SET_EXPECT( INTERNET_STATUS_RECEIVING_RESPONSE );
+        SET_EXPECT( INTERNET_STATUS_RESPONSE_RECEIVED );
+
+        bytes = 0xdeadbeef;
+        ret = InternetReadFile( req, buffer + count, min(16, sizeof(buffer) - count), &bytes );
+        if (!count) /* the first part should arrive immediately */
+            ok( ret, "InternetReadFile failed %u\n", GetLastError() );
+        if (!ret)
+        {
+            ok( GetLastError() == ERROR_IO_PENDING, "expected ERROR_IO_PENDING, got %u\n", GetLastError() );
+            ok( bytes == 0, "expected 0, got %u\n", bytes );
+            todo_wine
+            CHECK_NOTIFIED( INTERNET_STATUS_RECEIVING_RESPONSE );
+            SET_EXPECT( INTERNET_STATUS_REQUEST_COMPLETE );
+            if (!pending_reads++)
+            {
+                res = WaitForSingleObject( hCompleteEvent, 0 );
+                ok( res == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %u\n", res );
+                SetEvent( conn_wait_event );
+            }
+            res = WaitForSingleObject( hCompleteEvent, INFINITE );
+            ok( res == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", res );
+            ok( req_error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %u\n", req_error );
+            todo_wine_if( pending_reads > 1 )
+            ok( bytes != 0, "expected bytes != 0\n" );
+            CHECK_NOTIFIED( INTERNET_STATUS_RESPONSE_RECEIVED );
+            CHECK_NOTIFIED( INTERNET_STATUS_REQUEST_COMPLETE );
+        }
+
+        CLEAR_NOTIFIED( INTERNET_STATUS_RECEIVING_RESPONSE );
+        CLEAR_NOTIFIED( INTERNET_STATUS_RESPONSE_RECEIVED );
+        if (!bytes) break;
+    }
+
+    todo_wine
+    ok( pending_reads == 1, "expected 1 pending read, got %u\n", pending_reads );
+    ok( !strcmp(buffer, page1), "unexpected buffer content\n" );
+    close_async_handle( ses, hCompleteEvent, 2 );
+
+    CloseHandle( hCompleteEvent );
+    CloseHandle( conn_wait_event );
+}
+
+static void server_send_string(const char *msg)
+{
+    send(server_socket, msg, strlen(msg), 0);
+}
+
+static void WINAPI readex_callback(HINTERNET handle, DWORD_PTR context, DWORD status, void *info, DWORD info_size)
+{
+    switch(status) {
+    case INTERNET_STATUS_RECEIVING_RESPONSE:
+    case INTERNET_STATUS_RESPONSE_RECEIVED:
+        break;
+    default:
+        callback(handle, context, status, info, info_size);
+    }
+}
+
+static void open_read_test_request(int port, test_request_t *req, const char *response)
+{
+    BOOL ret;
+
+    req->session = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
+    ok(req->session != NULL, "InternetOpenA failed\n");
+    pInternetSetStatusCallbackA(req->session, readex_callback);
+
+    SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
+    req->connection = InternetConnectA(req->session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0xdeadbeef);
+    ok(req->connection != NULL, "InternetConnectA failed %u\n", GetLastError());
+    CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED );
+
+    SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
+    req->request = HttpOpenRequestA(req->connection, "GET", "/socket", NULL, NULL, NULL, INTERNET_FLAG_RELOAD, 0xdeadbeef);
+    ok(req->request != NULL, "HttpOpenRequestA failed %u\n", GetLastError());
+    CHECK_NOTIFIED( INTERNET_STATUS_HANDLE_CREATED );
+
+    SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
+    SET_OPTIONAL(INTERNET_STATUS_DETECTING_PROXY);
+    SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
+    SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
+    SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
+    SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
+
+    SetLastError(0xdeadbeef);
+    ret = HttpSendRequestA(req->request, NULL, 0, NULL, 0);
+    ok(!ret, "HttpSendRequestA unexpectedly succeeded\n");
+    ok(GetLastError() == ERROR_IO_PENDING, "expected ERROR_IO_PENDING, got %u\n", GetLastError());
+    ok(req_error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %u\n", req_error);
+
+    WaitForSingleObject(server_req_rec_event, INFINITE);
+
+    CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
+    CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
+    CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
+    CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
+    CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
+
+    SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
+
+    server_send_string(response);
+    WaitForSingleObject(hCompleteEvent, INFINITE);
+
+    CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
+    CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
+}
+
+#define readex_expect_sync_data_len(a,b,c,d,e,f) _readex_expect_sync_data_len(__LINE__,a,b,c,d,e,f)
+static void _readex_expect_sync_data_len(unsigned line, HINTERNET req, DWORD flags, INTERNET_BUFFERSW *buf,
+        DWORD buf_size, const char *exdata, DWORD len)
+{
+    BOOL ret;
+
+    SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
+
+    memset(buf->lpvBuffer, 0xff, buf_size);
+    buf->dwBufferLength = buf_size;
+    ret = InternetReadFileExW(req, buf, flags, 0xdeadbeef);
+    ok_(__FILE__,line)(ret, "InternetReadFileExW failed: %u\n", GetLastError());
+    ok_(__FILE__,line)(buf->dwBufferLength == len, "dwBufferLength = %u, expected %u\n", buf->dwBufferLength, len);
+    if(len && exdata)
+        ok_(__FILE__,line)(!memcmp(buf->lpvBuffer, exdata, len), "Unexpected data\n");
+
+    CLEAR_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
+}
+
+#define readex_expect_sync_data(a,b,c,d,e) _readex_expect_sync_data(__LINE__,a,b,c,d,e)
+static void _readex_expect_sync_data(unsigned line, HINTERNET req, DWORD flags, INTERNET_BUFFERSW *buf,
+        DWORD buf_size, const char *exdata)
+{
+    _readex_expect_sync_data_len(line, req, flags, buf, buf_size, exdata, strlen(exdata));
+}
+
+static void send_response_and_wait(const char *response, BOOL close_connection, INTERNET_BUFFERSW *buf)
+{
+    DWORD orig_size = buf->dwBufferLength;
+
+    SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
+
+    if(response)
+        server_send_string(response);
+
+    if(close_connection) {
+        char c;
+        SetEvent(conn_wait_event);
+        recv(server_socket, &c, 1, 0);
+    }
+
+    WaitForSingleObject(hCompleteEvent, INFINITE);
+
+    CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
+
+    /* If IRF_NO_WAIT is used, buffer is not changed. */
+    ok(buf->dwBufferLength == orig_size, "dwBufferLength = %u\n", buf->dwBufferLength);
+    ok(!*(int*)buf->lpvBuffer, "buffer data changed\n");
+}
+
+static void send_response_len_and_wait(unsigned len, BOOL close_connection, INTERNET_BUFFERSW *buf)
+{
+    char *response;
+
+    response = HeapAlloc(GetProcessHeap(), 0, len+1);
+    memset(response, 'x', len);
+    response[len] = 0;
+    send_response_and_wait(response, close_connection, buf);
+    HeapFree(GetProcessHeap(), 0, response);
+}
+
+static void readex_expect_async(HINTERNET req, DWORD flags, INTERNET_BUFFERSW *buf, DWORD buf_size)
+{
+    BOOL ret;
+
+    memset(buf->lpvBuffer, 0, buf_size);
+    buf->dwBufferLength = buf_size;
+    ret = InternetReadFileExW(req, buf, flags, 0xdeadbeef);
+    ok(!ret && GetLastError() == ERROR_IO_PENDING, "InternetReadFileExW returned %x (%u)\n", ret, GetLastError());
+    ok(buf->dwBufferLength == buf_size, "dwBufferLength = %u, expected %u\n", buf->dwBufferLength, buf_size);
+    ok(!*(int*)buf->lpvBuffer, "buffer data changed\n");
+}
+
+#define expect_data_available(a,b) _expect_data_available(__LINE__,a,b)
+static DWORD _expect_data_available(unsigned line, HINTERNET req, int exsize)
+{
+    DWORD size = 0;
+    BOOL res;
+
+    res = InternetQueryDataAvailable(req, &size, 0, 0);
+    ok_(__FILE__,line)(res, "InternetQueryDataAvailable failed: %u\n", GetLastError());
+    if(exsize != -1)
+        ok_(__FILE__,line)(size  == exsize, "size = %u, expected %u\n", size, exsize);
+
+    return size;
+}
+
+static void test_http_read(int port)
+{
+    INTERNET_BUFFERSW ib;
+    test_request_t req;
+    char buf[24000];
+    DWORD avail;
+
+    if(!is_ie7plus)
+        return;
+
+    hCompleteEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
+    conn_wait_event = CreateEventW(NULL, FALSE, FALSE, NULL);
+    server_req_rec_event = CreateEventW(NULL, FALSE, FALSE, NULL);
 
-    ctx = 0xdeadbeef;
-    size = sizeof(ctx);
-    ret = InternetQueryOptionA(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
-    ok(ret, "InternetQueryOption failed %u\n", GetLastError());
-    ok(ctx == 2, "expected 2 got %lu\n", ctx);
+    trace("Testing InternetReadFileExW with IRF_NO_WAIT flag...\n");
 
-    req = HttpOpenRequestA(con, NULL, "/test1", NULL, NULL, NULL, 0, 0);
-    ok(req != NULL, "HttpOpenRequest failed\n");
+    open_read_test_request(port, &req,
+                           "HTTP/1.1 200 OK\r\n"
+                           "Server: winetest\r\n"
+                           "\r\n"
+                           "xx");
 
-    ctx = 0xdeadbeef;
-    size = sizeof(ctx);
-    ret = InternetQueryOptionA(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
-    ok(ret, "InternetQueryOption failed %u\n", GetLastError());
-    ok(ctx == 0, "expected 0 got %lu\n", ctx);
+    memset(&ib, 0, sizeof(ib));
+    ib.dwStructSize = sizeof(ib);
+    ib.lpvBuffer = buf;
 
-    ctx = 3;
-    ret = InternetSetOptionA(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
-    ok(ret, "InternetSetOption failed %u\n", GetLastError());
+    readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "xx");
 
-    ctx = 0xdeadbeef;
-    size = sizeof(ctx);
-    ret = InternetQueryOptionA(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
-    ok(ret, "InternetQueryOption failed %u\n", GetLastError());
-    ok(ctx == 3, "expected 3 got %lu\n", ctx);
+    readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf));
+    send_response_and_wait("1234567890", FALSE, &ib);
+    readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, 5, "12345");
+    readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "67890");
 
-    size = sizeof(idsi);
-    ret = InternetQueryOptionA(req, INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO, &idsi, &size);
-    ok(ret, "InternetQueryOption failed %u\n", GetLastError());
+    readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf));
+    send_response_and_wait("12345", TRUE, &ib);
 
-    size = 0;
-    SetLastError(0xdeadbeef);
-    ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT, NULL, &size);
-    error = GetLastError();
-    ok(!ret, "InternetQueryOption succeeded\n");
-    ok(error == ERROR_INTERNET_INVALID_OPERATION, "expected ERROR_INTERNET_INVALID_OPERATION, got %u\n", error);
+    readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "12345");
+    readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "");
 
-    /* INTERNET_OPTION_PROXY */
-    SetLastError(0xdeadbeef);
-    ret = InternetQueryOptionA(ses, INTERNET_OPTION_PROXY, NULL, NULL);
-    error = GetLastError();
-    ok(!ret, "InternetQueryOption succeeded\n");
-    ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
+    close_async_handle(req.session, hCompleteEvent, 2);
 
-    SetLastError(0xdeadbeef);
-    ret = InternetQueryOptionA(ses, INTERNET_OPTION_PROXY, &ctx, NULL);
-    error = GetLastError();
-    ok(!ret, "InternetQueryOption succeeded\n");
-    ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
+    open_read_test_request(port, &req,
+                           "HTTP/1.1 200 OK\r\n"
+                           "Server: winetest\r\n"
+                           "Transfer-Encoding: chunked\r\n"
+                           "\r\n"
+                           "9\r\n123456789");
+    readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "123456789");
+    readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf));
 
-    size = 0;
-    SetLastError(0xdeadbeef);
-    ret = InternetQueryOptionA(ses, INTERNET_OPTION_PROXY, NULL, &size);
-    error = GetLastError();
-    ok(!ret, "InternetQueryOption succeeded\n");
-    ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
-    ok(size >= sizeof(INTERNET_PROXY_INFOA), "expected size to be greater or equal to the struct size\n");
+    send_response_and_wait("\r\n1\r\na\r\n1\r\nb\r", FALSE, &ib);
+    readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "ab");
+    readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf));
 
-    InternetCloseHandle(req);
-    InternetCloseHandle(con);
-    InternetCloseHandle(ses);
-}
+    send_response_and_wait("\n3\r\nab", FALSE, &ib);
+    readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "ab");
+    readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf));
 
-typedef struct {
-    const char *response_text;
-    int status_code;
-    const char *status_text;
-    const char *raw_headers;
-} http_status_test_t;
+    send_response_and_wait("c", FALSE, &ib);
+    readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "c");
+    readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf));
 
-static const http_status_test_t http_status_tests[] = {
-    {
-        "HTTP/1.1 200 OK\r\n"
-        "Content-Length: 1\r\n"
-        "\r\nx",
-        200,
-        "OK"
-    },
-    {
-        "HTTP/1.1 404 Fail\r\n"
-        "Content-Length: 1\r\n"
-        "\r\nx",
-        404,
-        "Fail"
-    },
-    {
-        "HTTP/1.1 200\r\n"
-        "Content-Length: 1\r\n"
-        "\r\nx",
-        200,
-        ""
-    },
-    {
-        "HTTP/1.1 410 \r\n"
-        "Content-Length: 1\r\n"
-        "\r\nx",
-        410,
-        ""
-    }
-};
+    send_response_and_wait("\r\n1\r\nx\r\n0\r\n\r\n", TRUE, &ib);
+    readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "x");
+    readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "");
 
-static void test_http_status(int port)
-{
-    HINTERNET ses, con, req;
-    char buf[1000];
-    DWORD i, size;
-    BOOL res;
+    close_async_handle(req.session, hCompleteEvent, 2);
 
-    for(i=0; i < sizeof(http_status_tests)/sizeof(*http_status_tests); i++) {
-        send_buffer = http_status_tests[i].response_text;
+    open_read_test_request(port, &req,
+                           "HTTP/1.1 200 OK\r\n"
+                           "Server: winetest\r\n"
+                           "Transfer-Encoding: chunked\r\n"
+                           "\r\n"
+                           "3\r\n123\r\n");
+    readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "123");
+    readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf));
 
-        ses = InternetOpenA(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
-        ok(ses != NULL, "InternetOpen failed\n");
+    send_response_and_wait("0\r\n\r\n", TRUE, &ib);
+    readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "");
 
-        con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
-        ok(con != NULL, "InternetConnect failed\n");
+    close_async_handle(req.session, hCompleteEvent, 2);
 
-        req = HttpOpenRequestA(con, NULL, "/send_from_buffer", NULL, NULL, NULL, 0, 0);
-        ok(req != NULL, "HttpOpenRequest failed\n");
+    trace("Testing InternetQueryDataAvailable...\n");
 
-        res = HttpSendRequestA(req, NULL, 0, NULL, 0);
-        ok(res, "HttpSendRequest failed\n");
+    open_read_test_request(port, &req,
+                           "HTTP/1.1 200 OK\r\n"
+                           "Server: winetest\r\n"
+                           "\r\n"
+                           "123");
+    expect_data_available(req.request, 3);
+    readex_expect_sync_data(req.request, IRF_NO_WAIT, &ib, sizeof(buf), "123");
+    readex_expect_async(req.request, IRF_NO_WAIT, &ib, sizeof(buf));
 
-        test_status_code(req, http_status_tests[i].status_code);
+    send_response_len_and_wait(20000, TRUE, &ib);
+    avail = expect_data_available(req.request, -1);
+    ok(avail < 17000, "avail = %u\n", avail);
 
-        size = sizeof(buf);
-        res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_TEXT, buf, &size, NULL);
-        ok(res, "HttpQueryInfo failed: %u\n", GetLastError());
-        ok(!strcmp(buf, http_status_tests[i].status_text), "[%u] Unexpected status text \"%s\", expected \"%s\"\n",
-           i, buf, http_status_tests[i].status_text);
+    SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
+    SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
+    close_async_handle(req.session, hCompleteEvent, 2);
+    todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
+    todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
 
-        InternetCloseHandle(req);
-        InternetCloseHandle(con);
-        InternetCloseHandle(ses);
-    }
+    CloseHandle(hCompleteEvent);
+    CloseHandle(conn_wait_event);
+    CloseHandle(server_req_rec_event);
 }
 
-static void test_cache_control_verb(int port)
+static void test_long_url(int port)
 {
-    HINTERNET session, connect, request;
+    char long_path[INTERNET_MAX_PATH_LENGTH*2] = "/echo_request?";
+    char buf[sizeof(long_path)*2], url[sizeof(buf)];
+    test_request_t req;
+    DWORD size, len;
     BOOL ret;
 
-    session = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
-    ok(session != NULL, "InternetOpen failed\n");
+    if(!is_ie7plus)
+        return;
 
-    connect = InternetConnectA(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
-    ok(connect != NULL, "InternetConnect failed\n");
+    memset(long_path+strlen(long_path), 'x', sizeof(long_path)-strlen(long_path));
+    long_path[sizeof(long_path)-1] = 0;
+    open_simple_request(&req, "localhost", port, NULL, long_path);
 
-    request = HttpOpenRequestA(connect, "RPC_OUT_DATA", "/test_cache_control_verb", NULL, NULL, NULL,
-                              INTERNET_FLAG_NO_CACHE_WRITE, 0);
-    ok(request != NULL, "HttpOpenRequest failed\n");
-    ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
-    ok(ret, "HttpSendRequest failed %u\n", GetLastError());
-    test_status_code(request, 200);
-    InternetCloseHandle(request);
+    ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
+    test_status_code(req.request, 200);
 
-    request = HttpOpenRequestA(connect, "POST", "/test_cache_control_verb", NULL, NULL, NULL,
-                              INTERNET_FLAG_NO_CACHE_WRITE, 0);
-    ok(request != NULL, "HttpOpenRequest failed\n");
-    ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
-    ok(ret, "HttpSendRequest failed %u\n", GetLastError());
-    test_status_code(request, 200);
-    InternetCloseHandle(request);
+    receive_simple_request(req.request, buf, sizeof(buf));
+    ok(strstr(buf, long_path) != NULL, "long pathnot found in %s\n", buf);
 
-    request = HttpOpenRequestA(connect, "HEAD", "/test_cache_control_verb", NULL, NULL, NULL,
-                              INTERNET_FLAG_NO_CACHE_WRITE, 0);
-    ok(request != NULL, "HttpOpenRequest failed\n");
-    ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
-    ok(ret, "HttpSendRequest failed %u\n", GetLastError());
-    test_status_code(request, 200);
-    InternetCloseHandle(request);
+    sprintf(url, "http://localhost:%u%s", port, long_path);
 
-    request = HttpOpenRequestA(connect, "GET", "/test_cache_control_verb", NULL, NULL, NULL,
-                              INTERNET_FLAG_NO_CACHE_WRITE, 0);
-    ok(request != NULL, "HttpOpenRequest failed\n");
-    ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
-    ok(ret, "HttpSendRequest failed %u\n", GetLastError());
-    test_status_code(request, 200);
-    InternetCloseHandle(request);
+    size = sizeof(buf);
+    ret = InternetQueryOptionA(req.request, INTERNET_OPTION_URL, buf, &size);
+    ok(ret, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
+    len = strlen(url);
+    ok(size == len, "size = %u, expected %u\n", size, len);
+    ok(!strcmp(buf, url), "Wrong URL %s, expected %s\n", buf, url);
 
-    InternetCloseHandle(connect);
-    InternetCloseHandle(session);
+    close_request(&req);
 }
 
 static void test_http_connection(void)
@@ -3981,7 +5160,7 @@ static void test_http_connection(void)
     test_basic_request(si.port, "GET", "/test6");
     test_basic_request(si.port, "GET", "/testF");
     test_connection_header(si.port);
-    test_http1_1(si.port);
+    test_header_override(si.port);
     test_cookie_header(si.port);
     test_basic_authentication(si.port);
     test_invalid_response_headers(si.port);
@@ -3998,6 +5177,13 @@ static void test_http_connection(void)
     test_connection_closing(si.port);
     test_cache_control_verb(si.port);
     test_successive_HttpSendRequest(si.port);
+    test_head_request(si.port);
+    test_request_content_length(si.port);
+    test_accept_encoding(si.port);
+    test_basic_auth_credentials_reuse(si.port);
+    test_async_read(si.port);
+    test_http_read(si.port);
+    test_long_url(si.port);
 
     /* send the basic request again to shutdown the server thread */
     test_basic_request(si.port, "GET", "/quit");
@@ -4022,15 +5208,11 @@ typedef struct {
 } cert_struct_test_t;
 
 static const cert_struct_test_t test_winehq_org_cert = {
-    "6JcR7G3G8tgjkeoMcitK-bTbzcpumDSy\r\n"
-    "GT98380011\r\n"
-    "See www.rapidssl.com/resources/cps (c)14\r\n"
-    "Domain Control Validated - RapidSSL(R)\r\n"
     "*.winehq.org",
 
     "US\r\n"
-    "\"GeoTrust, Inc.\"\r\n"
-    "RapidSSL CA"
+    "GeoTrust Inc.\r\n"
+    "RapidSSL SHA256 CA"
 };
 
 static const cert_struct_test_t test_winehq_com_cert = {
@@ -4066,7 +5248,7 @@ static void test_cert_struct(HINTERNET req, const cert_struct_test_t *test)
     ok(!info.lpszSignatureAlgName, "lpszSignatureAlgName = %s\n", info.lpszSignatureAlgName);
     ok(!info.lpszEncryptionAlgName, "lpszEncryptionAlgName = %s\n", info.lpszEncryptionAlgName);
     ok(!info.lpszProtocolName, "lpszProtocolName = %s\n", info.lpszProtocolName);
-    ok(info.dwKeySize == 128, "dwKeySize = %u\n", info.dwKeySize);
+    ok(info.dwKeySize >= 128 && info.dwKeySize <= 256, "dwKeySize = %u\n", info.dwKeySize);
 
     release_cert_info(&info);
 }
@@ -4095,12 +5277,12 @@ static void _test_security_info(unsigned line, const char *urlc, DWORD error, DW
         CertFreeCertificateChain(chain);
     }else {
         ok_(__FILE__,line)(!res && GetLastError() == error,
-                           "InternetGetSecurityInfoByURLA returned: %x(%u), exected %u\n", res, GetLastError(), error);
+                           "InternetGetSecurityInfoByURLA returned: %x(%u), expected %u\n", res, GetLastError(), error);
     }
 }
 
-#define test_secflags_option(a,b) _test_secflags_option(__LINE__,a,b)
-static void _test_secflags_option(unsigned line, HINTERNET req, DWORD ex_flags)
+#define test_secflags_option(a,b,c) _test_secflags_option(__LINE__,a,b,c)
+static void _test_secflags_option(unsigned line, HINTERNET req, DWORD ex_flags, DWORD opt_flags)
 {
     DWORD flags, size;
     BOOL res;
@@ -4109,14 +5291,16 @@ static void _test_secflags_option(unsigned line, HINTERNET req, DWORD ex_flags)
     size = sizeof(flags);
     res = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
     ok_(__FILE__,line)(res, "InternetQueryOptionW(INTERNET_OPTION_SECURITY_FLAGS) failed: %u\n", GetLastError());
-    ok_(__FILE__,line)(flags == ex_flags, "INTERNET_OPTION_SECURITY_FLAGS flags = %x, expected %x\n", flags, ex_flags);
+    ok_(__FILE__,line)((flags & ~opt_flags) == ex_flags, "INTERNET_OPTION_SECURITY_FLAGS flags = %x, expected %x\n",
+                       flags, ex_flags);
 
     /* Option 98 is undocumented and seems to be the same as INTERNET_OPTION_SECURITY_FLAGS */
     flags = 0xdeadbeef;
     size = sizeof(flags);
     res = InternetQueryOptionW(req, 98, &flags, &size);
     ok_(__FILE__,line)(res, "InternetQueryOptionW(98) failed: %u\n", GetLastError());
-    ok_(__FILE__,line)(flags == ex_flags, "INTERNET_OPTION_SECURITY_FLAGS(98) flags = %x, expected %x\n", flags, ex_flags);
+    ok_(__FILE__,line)((flags & ~opt_flags) == ex_flags, "INTERNET_OPTION_SECURITY_FLAGS(98) flags = %x, expected %x\n",
+                       flags, ex_flags);
 }
 
 #define set_secflags(a,b,c) _set_secflags(__LINE__,a,b,c)
@@ -4169,17 +5353,17 @@ static void test_security_flags(void)
         return;
     }
 
-    test_secflags_option(req, 0);
+    test_secflags_option(req, 0, 0);
     test_security_info("https://test.winehq.com/data/some_file.html?q", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
 
     set_secflags(req, TRUE, SECURITY_FLAG_IGNORE_REVOCATION);
-    test_secflags_option(req, SECURITY_FLAG_IGNORE_REVOCATION);
+    test_secflags_option(req, SECURITY_FLAG_IGNORE_REVOCATION, 0);
 
     set_secflags(req, TRUE, SECURITY_FLAG_IGNORE_CERT_CN_INVALID);
-    test_secflags_option(req, SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_CERT_CN_INVALID);
+    test_secflags_option(req, SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_CERT_CN_INVALID, 0);
 
     set_secflags(req, FALSE, SECURITY_FLAG_IGNORE_UNKNOWN_CA);
-    test_secflags_option(req, SECURITY_FLAG_IGNORE_UNKNOWN_CA|SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_CERT_CN_INVALID);
+    test_secflags_option(req, SECURITY_FLAG_IGNORE_UNKNOWN_CA|SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_CERT_CN_INVALID, 0);
 
     flags = SECURITY_FLAG_IGNORE_CERT_CN_INVALID|SECURITY_FLAG_SECURE;
     res = InternetSetOptionW(req, 99, &flags, sizeof(flags));
@@ -4189,6 +5373,10 @@ static void test_security_flags(void)
     SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
+    SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION); /* IE11 calls it, it probably reconnects. */
+    SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED); /* IE11 */
+    SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER); /* IE11 */
+    SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER); /* IE11 */
     SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
     SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
     SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
@@ -4205,8 +5393,10 @@ static void test_security_flags(void)
 
     CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
     CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
-    CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
-    CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
+    CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTING_TO_SERVER, 2);
+    CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTED_TO_SERVER, 2);
+    CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
+    CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
     CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
     CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
     CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
@@ -4217,7 +5407,7 @@ static void test_security_flags(void)
 
     test_request_flags(req, 0);
     test_secflags_option(req, SECURITY_FLAG_SECURE|SECURITY_FLAG_IGNORE_UNKNOWN_CA
-            |SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_CERT_CN_INVALID|SECURITY_FLAG_STRENGTH_STRONG);
+            |SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_CERT_CN_INVALID|SECURITY_FLAG_STRENGTH_STRONG, 0);
 
     res = InternetReadFile(req, buf, sizeof(buf), &size);
     ok(res, "InternetReadFile failed: %u\n", GetLastError());
@@ -4240,6 +5430,10 @@ static void test_security_flags(void)
 
     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
+    SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION); /* IE11 calls it, it probably reconnects. */
+    SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED); /* IE11 */
+    SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER); /* IE11 */
+    SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER); /* IE11 */
     SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
     SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
     SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
@@ -4277,10 +5471,10 @@ static void test_security_flags(void)
     }
     HeapFree(GetProcessHeap(), 0, cert);
 
-    CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
-    CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
-    CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
-    CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
+    CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTING_TO_SERVER, 2);
+    CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTED_TO_SERVER, 2);
+    CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
+    CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
     CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
     CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
     CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
@@ -4298,10 +5492,12 @@ static void test_security_flags(void)
     ok(!res && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfoA(HTTP_QUERY_CONTENT_ENCODING) failed: %u\n", GetLastError());
 
     test_request_flags(req, 8);
-    test_secflags_option(req, 0x800000);
+    /* IE11 finds both rev failure and invalid CA. Previous versions required rev failure
+       to be ignored before invalid CA was reported. */
+    test_secflags_option(req, _SECURITY_FLAG_CERT_REV_FAILED, _SECURITY_FLAG_CERT_INVALID_CA);
 
     set_secflags(req, FALSE, SECURITY_FLAG_IGNORE_REVOCATION);
-    test_secflags_option(req, 0x800000|SECURITY_FLAG_IGNORE_REVOCATION);
+    test_secflags_option(req, _SECURITY_FLAG_CERT_REV_FAILED|SECURITY_FLAG_IGNORE_REVOCATION, _SECURITY_FLAG_CERT_INVALID_CA);
 
     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
@@ -4326,16 +5522,20 @@ static void test_security_flags(void)
     CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
 
     test_request_flags(req, INTERNET_REQFLAG_NO_HEADERS);
-    test_secflags_option(req, SECURITY_FLAG_IGNORE_REVOCATION|0x1800000);
+    test_secflags_option(req, SECURITY_FLAG_IGNORE_REVOCATION|_SECURITY_FLAG_CERT_REV_FAILED|_SECURITY_FLAG_CERT_INVALID_CA, 0);
     test_security_info("https://test.winehq.com/data/some_file.html?q", ERROR_INTERNET_ITEM_NOT_FOUND, 0);
 
     set_secflags(req, FALSE, SECURITY_FLAG_IGNORE_UNKNOWN_CA);
-    test_secflags_option(req, 0x1800000|SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_UNKNOWN_CA
-            |SECURITY_FLAG_IGNORE_REVOCATION);
+    test_secflags_option(req, _SECURITY_FLAG_CERT_INVALID_CA|_SECURITY_FLAG_CERT_REV_FAILED
+            |SECURITY_FLAG_IGNORE_REVOCATION|SECURITY_FLAG_IGNORE_UNKNOWN_CA, 0);
     test_http_version(req);
 
     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
+    SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION); /* IE11 calls it, it probably reconnects. */
+    SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED); /* IE11 */
+    SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER); /* IE11 */
+    SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER); /* IE11 */
     SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
     SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
     SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
@@ -4350,8 +5550,10 @@ static void test_security_flags(void)
     WaitForSingleObject(hCompleteEvent, INFINITE);
     ok(req_error == ERROR_SUCCESS, "req_error = %d\n", req_error);
 
-    CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
-    CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
+    CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTING_TO_SERVER, 2);
+    CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTED_TO_SERVER, 2);
+    CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
+    CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
     CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
     CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
     CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
@@ -4362,10 +5564,11 @@ static void test_security_flags(void)
 
     test_request_flags(req, 0);
     test_secflags_option(req, SECURITY_FLAG_SECURE|SECURITY_FLAG_IGNORE_UNKNOWN_CA|SECURITY_FLAG_IGNORE_REVOCATION
-            |SECURITY_FLAG_STRENGTH_STRONG|0x1800000);
+            |SECURITY_FLAG_STRENGTH_STRONG|_SECURITY_FLAG_CERT_REV_FAILED|_SECURITY_FLAG_CERT_INVALID_CA, 0);
 
     test_cert_struct(req, &test_winehq_com_cert);
-    test_security_info("https://test.winehq.com/data/some_file.html?q", 0, 0x1800000);
+    test_security_info("https://test.winehq.com/data/some_file.html?q", 0,
+            _SECURITY_FLAG_CERT_INVALID_CA|_SECURITY_FLAG_CERT_REV_FAILED);
 
     res = InternetReadFile(req, buf, sizeof(buf), &size);
     ok(res, "InternetReadFile failed: %u\n", GetLastError());
@@ -4398,11 +5601,15 @@ static void test_security_flags(void)
     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
 
     test_secflags_option(req, SECURITY_FLAG_SECURE|SECURITY_FLAG_IGNORE_UNKNOWN_CA|SECURITY_FLAG_STRENGTH_STRONG
-           |SECURITY_FLAG_IGNORE_REVOCATION|0x1800000);
+            |SECURITY_FLAG_IGNORE_REVOCATION|_SECURITY_FLAG_CERT_REV_FAILED|_SECURITY_FLAG_CERT_INVALID_CA, 0);
     test_http_version(req);
 
     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
+    SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION); /* IE11 calls it, it probably reconnects. */
+    SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED); /* IE11 */
+    SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER); /* IE11 */
+    SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER); /* IE11 */
     SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
     SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
     SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
@@ -4416,8 +5623,10 @@ static void test_security_flags(void)
     WaitForSingleObject(hCompleteEvent, INFINITE);
     ok(req_error == ERROR_SUCCESS, "req_error = %d\n", req_error);
 
-    CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
-    CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
+    CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTING_TO_SERVER, 2);
+    CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTED_TO_SERVER, 2);
+    CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
+    CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
     CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
     CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
     CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
@@ -4427,7 +5636,7 @@ static void test_security_flags(void)
 
     test_request_flags(req, 0);
     test_secflags_option(req, SECURITY_FLAG_SECURE|SECURITY_FLAG_IGNORE_UNKNOWN_CA|SECURITY_FLAG_STRENGTH_STRONG
-            |SECURITY_FLAG_IGNORE_REVOCATION|0x1800000);
+            |SECURITY_FLAG_IGNORE_REVOCATION|_SECURITY_FLAG_CERT_REV_FAILED|_SECURITY_FLAG_CERT_INVALID_CA, 0);
 
     res = InternetReadFile(req, buf, sizeof(buf), &size);
     ok(res, "InternetReadFile failed: %u\n", GetLastError());
@@ -4467,21 +5676,20 @@ static void test_secure_connection(void)
     ok(req != NULL, "HttpOpenRequest failed\n");
 
     ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
-    ok(ret, "HttpSendRequest failed: %d\n", GetLastError());
+    ok(ret || broken(GetLastError() == ERROR_INTERNET_CANNOT_CONNECT),
+                     "HttpSendRequest failed: %d\n", GetLastError());
+    if (!ret)
+    {
+        win_skip("Cannot connect to https.\n");
+        goto done;
+    }
 
     size = sizeof(flags);
     ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
     ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
     ok(flags & SECURITY_FLAG_SECURE, "expected secure flag to be set\n");
 
-    if (!winetest_interactive)
-    {
-        skip("test_cert_struct(req, &test_winehq_org_cert), ROSTESTS-121\n");
-    }
-    else
-    {
-        test_cert_struct(req, &test_winehq_org_cert);
-    }
+    test_cert_struct(req, &test_winehq_org_cert);
 
     /* Querying the same option through InternetQueryOptionW still results in
      * ASCII strings being returned.
@@ -4599,6 +5807,7 @@ static void test_secure_connection(void)
     }
     HeapFree(GetProcessHeap(), 0, certificate_structW);
 
+done:
     InternetCloseHandle(req);
     InternetCloseHandle(con);
     InternetCloseHandle(ses);
@@ -4633,7 +5842,6 @@ static void test_user_agent_header(void)
     ret = HttpQueryInfoA(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
     err = GetLastError();
     ok(ret, "HttpQueryInfo failed\n");
-    ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
 
     InternetCloseHandle(req);
 
@@ -4744,10 +5952,8 @@ static void test_open_url_async(void)
      * other versions never do. They also hang of following tests. We disable it for everything older
      * than IE7.
      */
-    if(!pInternetGetSecurityInfoByURLA) {
-        win_skip("Skipping async open on too old wininet version.\n");
+    if(!is_ie7plus)
         return;
-    }
 
     ctx.req = NULL;
     ctx.event = CreateEventA(NULL, TRUE, FALSE, "Z:_home_hans_jaman-installer.exe_ev1");
@@ -5010,7 +6216,7 @@ static const struct notification_data notification_data[] = {
         sizeof(async_send_request_ex_test)/sizeof(async_send_request_ex_test[0]),
         "POST",
         "test.winehq.org",
-        "tests/posttest.php",
+        "tests/post.php",
         "Public ID=codeweavers"
     },
     {
@@ -5018,7 +6224,7 @@ static const struct notification_data notification_data[] = {
         sizeof(async_send_request_ex_test)/sizeof(async_send_request_ex_test[0]),
         "POST",
         "test.winehq.org",
-        "tests/posttest.php"
+        "tests/post.php"
     },
     {
         async_send_request_ex_resolve_failure_test,
@@ -5139,6 +6345,7 @@ static void test_async_HttpSendRequestEx(const struct notification_data *nd)
     WaitForSingleObject( info.wait, 10000 );
     Sleep(100);
     CloseHandle( info.wait );
+    DeleteCriticalSection( &notification_cs );
 }
 
 static HINTERNET closetest_session, closetest_req, closetest_conn;
@@ -5209,36 +6416,31 @@ static void test_InternetCloseHandle(void)
 
 static void test_connection_failure(void)
 {
-    HINTERNET session, connect, request;
+    test_request_t req;
     DWORD error;
     BOOL ret;
 
-    session = InternetOpenA("winetest", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
-    ok(session != NULL, "failed to get session handle\n");
-
-    connect = InternetConnectA(session, "localhost", 1, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
-    ok(connect != NULL, "failed to get connection handle\n");
-
-    request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, NULL, 0, 0);
-    ok(request != NULL, "failed to get request handle\n");
+    open_simple_request(&req, "localhost", 1, NULL, "/");
 
     SetLastError(0xdeadbeef);
-    ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
+    ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
     error = GetLastError();
     ok(!ret, "unexpected success\n");
     ok(error == ERROR_INTERNET_CANNOT_CONNECT, "wrong error %u\n", error);
 
-    InternetCloseHandle(request);
-    InternetCloseHandle(connect);
-    InternetCloseHandle(session);
+    close_request(&req);
 }
 
 static void test_default_service_port(void)
 {
     HINTERNET session, connect, request;
-    DWORD error;
+    DWORD size, error;
+    char buffer[128];
     BOOL ret;
 
+    if(!is_ie7plus)
+        return;
+
     session = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
     ok(session != NULL, "InternetOpen failed\n");
 
@@ -5256,6 +6458,50 @@ static void test_default_service_port(void)
     ok(error == ERROR_INTERNET_SECURITY_CHANNEL_ERROR || error == ERROR_INTERNET_CANNOT_CONNECT,
        "got %u\n", error);
 
+    size = sizeof(buffer);
+    memset(buffer, 0, sizeof(buffer));
+    ret = HttpQueryInfoA(request, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
+    ok(ret, "HttpQueryInfo failed with error %u\n", GetLastError());
+    ok(!strcmp(buffer, "test.winehq.org:80"), "Expected test.winehg.org:80, got '%s'\n", buffer);
+
+    InternetCloseHandle(request);
+    InternetCloseHandle(connect);
+
+    connect = InternetConnectA(session, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER, NULL, NULL,
+                               INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0);
+    ok(connect != NULL, "InternetConnect failed\n");
+
+    request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0);
+    ok(request != NULL, "HttpOpenRequest failed\n");
+
+    ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
+
+    size = sizeof(buffer);
+    memset(buffer, 0, sizeof(buffer));
+    ret = HttpQueryInfoA(request, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
+    ok(ret, "HttpQueryInfo failed with error %u\n", GetLastError());
+    ok(!strcmp(buffer, "test.winehq.org"), "Expected test.winehg.org, got '%s'\n", buffer);
+
+    InternetCloseHandle(request);
+    InternetCloseHandle(connect);
+
+    connect = InternetConnectA(session, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER, NULL, NULL,
+                               INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0);
+    ok(connect != NULL, "InternetConnect failed\n");
+
+    request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, NULL, 0, 0);
+    ok(request != NULL, "HttpOpenRequest failed\n");
+
+    ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
+    ok(ret, "HttpSendRequest failed with error %u\n", GetLastError());
+
+    size = sizeof(buffer);
+    memset(buffer, 0, sizeof(buffer));
+    ret = HttpQueryInfoA(request, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
+    ok(ret, "HttpQueryInfo failed with error %u\n", GetLastError());
+    ok(!strcmp(buffer, "test.winehq.org:443"), "Expected test.winehg.org:443, got '%s'\n", buffer);
+
     InternetCloseHandle(request);
     InternetCloseHandle(connect);
     InternetCloseHandle(session);
@@ -5299,6 +6545,64 @@ static void init_status_tests(void)
 #undef STATUS_STRING
 }
 
+static void WINAPI header_cb( HINTERNET handle, DWORD_PTR ctx, DWORD status, LPVOID info, DWORD len )
+{
+    BOOL ret;
+    DWORD index, size;
+    char buf[256];
+
+    if (status == INTERNET_STATUS_SENDING_REQUEST)
+    {
+        ret = HttpAddRequestHeadersA( handle, "winetest: winetest", ~0u, HTTP_ADDREQ_FLAG_ADD );
+        ok( ret, "HttpAddRequestHeadersA failed %u\n", GetLastError() );
+        SetEvent( (HANDLE)ctx );
+    }
+    else if (status == INTERNET_STATUS_REQUEST_SENT)
+    {
+        index = 0;
+        size = sizeof(buf);
+        ret = HttpQueryInfoA( handle, HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
+                              buf, &size, &index );
+        ok( ret, "HttpQueryInfoA failed %u\n", GetLastError() );
+        ok( strstr( buf, "winetest: winetest" ) != NULL, "header missing\n" );
+        SetEvent( (HANDLE)ctx );
+    }
+}
+
+static void test_concurrent_header_access(void)
+{
+    HINTERNET ses, con, req;
+    DWORD err;
+    BOOL ret;
+    HANDLE wait = CreateEventW( NULL, FALSE, FALSE, NULL );
+
+    ses = InternetOpenA( "winetest", 0, NULL, NULL, INTERNET_FLAG_ASYNC );
+    ok( ses != NULL, "InternetOpenA failed\n" );
+
+    con = InternetConnectA( ses, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
+                            INTERNET_SERVICE_HTTP, 0, 0 );
+    ok( con != NULL, "InternetConnectA failed %u\n", GetLastError() );
+
+    req = HttpOpenRequestA( con, NULL, "/", NULL, NULL, NULL, 0, (DWORD_PTR)wait );
+    ok( req != NULL, "HttpOpenRequestA failed %u\n", GetLastError() );
+
+    pInternetSetStatusCallbackA( req, header_cb );
+
+    SetLastError( 0xdeadbeef );
+    ret = HttpSendRequestA( req, NULL, 0, NULL, 0 );
+    err = GetLastError();
+    ok( !ret, "HttpSendRequestA succeeded\n" );
+    ok( err == ERROR_IO_PENDING, "got %u\n", ERROR_IO_PENDING );
+
+    WaitForSingleObject( wait, 5000 );
+    WaitForSingleObject( wait, 5000 );
+
+    InternetCloseHandle( req );
+    InternetCloseHandle( con );
+    InternetCloseHandle( ses );
+    CloseHandle( wait );
+}
+
 START_TEST(http)
 {
     HMODULE hdll;
@@ -5313,12 +6617,16 @@ START_TEST(http)
     pInternetSetStatusCallbackW = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackW");
     pInternetGetSecurityInfoByURLA = (void*)GetProcAddress(hdll, "InternetGetSecurityInfoByURLA");
 
+    if(!pInternetGetSecurityInfoByURLA) {
+        is_ie7plus = FALSE;
+        win_skip("IE6 found. It's too old for some tests.\n");
+    }
+
     init_status_tests();
     test_InternetCloseHandle();
     InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[0]);
     InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[1]);
     InternetReadFile_test(0, &test_data[1]);
-    first_connection_to_test_url = TRUE;
     InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[2]);
     test_security_flags();
     InternetReadFile_test(0, &test_data[2]);
@@ -5342,4 +6650,5 @@ START_TEST(http)
     InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[3]);
     test_connection_failure();
     test_default_service_port();
+    test_concurrent_header_access();
 }