dmusic: COM cleanup of IDirectMusic8.
[wine] / dlls / wininet / tests / http.c
1 /*
2  * Wininet - Http tests
3  *
4  * Copyright 2002 Aric Stewart
5  * Copyright 2004 Mike McCormack
6  * Copyright 2005 Hans Leidekker
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wininet.h"
30 #include "winsock.h"
31
32 #include "wine/test.h"
33
34 #define TEST_URL "http://test.winehq.org/tests/hello.html"
35
36 static BOOL first_connection_to_test_url = TRUE;
37
38 /* Adapted from dlls/urlmon/tests/protocol.c */
39
40 #define SET_EXPECT2(status, num) \
41     expect[status] = num
42
43 #define SET_EXPECT(status) \
44     SET_EXPECT2(status, 1)
45
46 #define SET_OPTIONAL2(status, num) \
47     optional[status] = num
48
49 #define SET_OPTIONAL(status) \
50     SET_OPTIONAL2(status, 1)
51
52 /* SET_WINE_ALLOW's should be used with an appropriate
53  * todo_wine CHECK_NOTIFIED at a later point in the code */
54 #define SET_WINE_ALLOW2(status, num) \
55     wine_allow[status] = num
56
57 #define SET_WINE_ALLOW(status) \
58     SET_WINE_ALLOW2(status, 1)
59
60 #define CHECK_EXPECT(status) \
61     do { \
62         if (!expect[status] && !optional[status] && wine_allow[status]) \
63         { \
64             todo_wine ok(expect[status], "unexpected status %d (%s)\n", status, \
65                          status < MAX_INTERNET_STATUS && status_string[status] ? \
66                          status_string[status] : "unknown");            \
67             wine_allow[status]--; \
68         } \
69         else \
70         { \
71             ok(expect[status] || optional[status], "unexpected status %d (%s)\n", status,   \
72                status < MAX_INTERNET_STATUS && status_string[status] ? \
73                status_string[status] : "unknown");                      \
74             if (expect[status]) expect[status]--; \
75             else optional[status]--; \
76         } \
77         notified[status]++; \
78     }while(0)
79
80 /* CLEAR_NOTIFIED used in cases when notification behavior
81  * differs between Windows versions */
82 #define CLEAR_NOTIFIED(status) \
83     expect[status] = optional[status] = wine_allow[status] = notified[status] = 0;
84
85 #define CHECK_NOTIFIED2(status, num) \
86     do { \
87         ok(notified[status] + optional[status] == (num), \
88            "expected status %d (%s) %d times, received %d times\n", \
89            status, status < MAX_INTERNET_STATUS && status_string[status] ? \
90            status_string[status] : "unknown", (num), notified[status]); \
91         CLEAR_NOTIFIED(status);                                         \
92     }while(0)
93
94 #define CHECK_NOTIFIED(status) \
95     CHECK_NOTIFIED2(status, 1)
96
97 #define CHECK_NOT_NOTIFIED(status) \
98     CHECK_NOTIFIED2(status, 0)
99
100 #define MAX_INTERNET_STATUS (INTERNET_STATUS_COOKIE_HISTORY+1)
101 static int expect[MAX_INTERNET_STATUS], optional[MAX_INTERNET_STATUS],
102     wine_allow[MAX_INTERNET_STATUS], notified[MAX_INTERNET_STATUS];
103 static const char *status_string[MAX_INTERNET_STATUS];
104
105 static HANDLE hCompleteEvent, conn_close_event;
106 static DWORD req_error;
107
108 #define TESTF_REDIRECT      0x01
109 #define TESTF_COMPRESSED    0x02
110 #define TESTF_ALLOW_COOKIE  0x04
111 #define TESTF_CHUNKED       0x08
112
113 typedef struct {
114     const char *url;
115     const char *redirected_url;
116     const char *host;
117     const char *path;
118     const char *headers;
119     DWORD flags;
120     const char *post_data;
121     const char *content;
122 } test_data_t;
123
124 static const test_data_t test_data[] = {
125     {
126         "http://test.winehq.org/tests/data.php",
127         "http://test.winehq.org/tests/data.php",
128         "test.winehq.org",
129         "/tests/data.php",
130         "",
131         TESTF_CHUNKED|TESTF_ALLOW_COOKIE
132     },
133     {
134         "http://test.winehq.org/tests/redirect",
135         "http://test.winehq.org/tests/hello.html",
136         "test.winehq.org",
137         "/tests/redirect",
138         "",
139         TESTF_REDIRECT
140     },
141     {
142         "http://www.codeweavers.com/",
143         "http://www.codeweavers.com/",
144         "www.codeweavers.com",
145         "",
146         "Accept-Encoding: gzip, deflate",
147         TESTF_COMPRESSED|TESTF_ALLOW_COOKIE
148     },
149     {
150         "http://test.winehq.org/tests/post.php",
151         "http://test.winehq.org/tests/post.php",
152         "test.winehq.org",
153         "/tests/post.php",
154         "Content-Type: application/x-www-form-urlencoded",
155         TESTF_ALLOW_COOKIE,
156         "mode=Test",
157         "mode => Test\n"
158     }
159 };
160
161 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET ,INTERNET_STATUS_CALLBACK);
162
163 static BOOL proxy_active(void)
164 {
165     HKEY internet_settings;
166     DWORD proxy_enable;
167     DWORD size;
168
169     if (RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
170                       0, KEY_QUERY_VALUE, &internet_settings) != ERROR_SUCCESS)
171         return FALSE;
172
173     size = sizeof(DWORD);
174     if (RegQueryValueExA(internet_settings, "ProxyEnable", NULL, NULL, (LPBYTE) &proxy_enable, &size) != ERROR_SUCCESS)
175         proxy_enable = 0;
176
177     RegCloseKey(internet_settings);
178
179     return proxy_enable != 0;
180 }
181
182 static int close_handle_cnt;
183
184 static VOID WINAPI callback(
185      HINTERNET hInternet,
186      DWORD_PTR dwContext,
187      DWORD dwInternetStatus,
188      LPVOID lpvStatusInformation,
189      DWORD dwStatusInformationLength
190 )
191 {
192     CHECK_EXPECT(dwInternetStatus);
193     switch (dwInternetStatus)
194     {
195         case INTERNET_STATUS_RESOLVING_NAME:
196             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESOLVING_NAME \"%s\" %d\n",
197                 GetCurrentThreadId(), hInternet, dwContext,
198                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
199             *(LPSTR)lpvStatusInformation = '\0';
200             break;
201         case INTERNET_STATUS_NAME_RESOLVED:
202             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_NAME_RESOLVED \"%s\" %d\n",
203                 GetCurrentThreadId(), hInternet, dwContext,
204                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
205             *(LPSTR)lpvStatusInformation = '\0';
206             break;
207         case INTERNET_STATUS_CONNECTING_TO_SERVER:
208             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %d\n",
209                 GetCurrentThreadId(), hInternet, dwContext,
210                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
211             *(LPSTR)lpvStatusInformation = '\0';
212             break;
213         case INTERNET_STATUS_CONNECTED_TO_SERVER:
214             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %d\n",
215                 GetCurrentThreadId(), hInternet, dwContext,
216                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
217             *(LPSTR)lpvStatusInformation = '\0';
218             break;
219         case INTERNET_STATUS_SENDING_REQUEST:
220             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_SENDING_REQUEST %p %d\n",
221                 GetCurrentThreadId(), hInternet, dwContext,
222                 lpvStatusInformation,dwStatusInformationLength);
223             break;
224         case INTERNET_STATUS_REQUEST_SENT:
225             ok(dwStatusInformationLength == sizeof(DWORD),
226                 "info length should be sizeof(DWORD) instead of %d\n",
227                 dwStatusInformationLength);
228             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_SENT 0x%x %d\n",
229                 GetCurrentThreadId(), hInternet, dwContext,
230                 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
231             break;
232         case INTERNET_STATUS_RECEIVING_RESPONSE:
233             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RECEIVING_RESPONSE %p %d\n",
234                 GetCurrentThreadId(), hInternet, dwContext,
235                 lpvStatusInformation,dwStatusInformationLength);
236             break;
237         case INTERNET_STATUS_RESPONSE_RECEIVED:
238             ok(dwStatusInformationLength == sizeof(DWORD),
239                 "info length should be sizeof(DWORD) instead of %d\n",
240                 dwStatusInformationLength);
241             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESPONSE_RECEIVED 0x%x %d\n",
242                 GetCurrentThreadId(), hInternet, dwContext,
243                 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
244             break;
245         case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
246             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %d\n",
247                 GetCurrentThreadId(), hInternet,dwContext,
248                 lpvStatusInformation,dwStatusInformationLength);
249             break;
250         case INTERNET_STATUS_PREFETCH:
251             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_PREFETCH %p %d\n",
252                 GetCurrentThreadId(), hInternet, dwContext,
253                 lpvStatusInformation,dwStatusInformationLength);
254             break;
255         case INTERNET_STATUS_CLOSING_CONNECTION:
256             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CLOSING_CONNECTION %p %d\n",
257                 GetCurrentThreadId(), hInternet, dwContext,
258                 lpvStatusInformation,dwStatusInformationLength);
259             break;
260         case INTERNET_STATUS_CONNECTION_CLOSED:
261             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTION_CLOSED %p %d\n",
262                 GetCurrentThreadId(), hInternet, dwContext,
263                 lpvStatusInformation,dwStatusInformationLength);
264             break;
265         case INTERNET_STATUS_HANDLE_CREATED:
266             ok(dwStatusInformationLength == sizeof(HINTERNET),
267                 "info length should be sizeof(HINTERNET) instead of %d\n",
268                 dwStatusInformationLength);
269             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CREATED %p %d\n",
270                 GetCurrentThreadId(), hInternet, dwContext,
271                 *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
272             CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
273             SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
274             break;
275         case INTERNET_STATUS_HANDLE_CLOSING:
276             ok(dwStatusInformationLength == sizeof(HINTERNET),
277                 "info length should be sizeof(HINTERNET) instead of %d\n",
278                 dwStatusInformationLength);
279             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
280                 GetCurrentThreadId(), hInternet, dwContext,
281                 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
282             if(!--close_handle_cnt)
283                 SetEvent(hCompleteEvent);
284             break;
285         case INTERNET_STATUS_REQUEST_COMPLETE:
286         {
287             INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
288             ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
289                 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
290                 dwStatusInformationLength);
291             ok(iar->dwResult == 1 || iar->dwResult == 0, "iar->dwResult = %ld\n", iar->dwResult);
292             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%ld,%d} %d\n",
293                 GetCurrentThreadId(), hInternet, dwContext,
294                 iar->dwResult,iar->dwError,dwStatusInformationLength);
295             req_error = iar->dwError;
296             SetEvent(hCompleteEvent);
297             break;
298         }
299         case INTERNET_STATUS_REDIRECT:
300             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
301                 GetCurrentThreadId(), hInternet, dwContext,
302                 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
303             *(LPSTR)lpvStatusInformation = '\0';
304             CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
305             SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
306             break;
307         case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
308             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
309                 GetCurrentThreadId(), hInternet, dwContext,
310                 lpvStatusInformation, dwStatusInformationLength);
311             break;
312         default:
313             trace("%04x:Callback %p 0x%lx %d %p %d\n",
314                 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
315                 lpvStatusInformation, dwStatusInformationLength);
316     }
317 }
318
319 static void close_async_handle(HINTERNET handle, HANDLE complete_event, int handle_cnt)
320 {
321     BOOL res;
322
323     close_handle_cnt = handle_cnt;
324
325     SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, handle_cnt);
326     res = InternetCloseHandle(handle);
327     ok(res, "InternetCloseHandle failed: %u\n", GetLastError());
328     WaitForSingleObject(hCompleteEvent, INFINITE);
329     CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, handle_cnt);
330     SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, handle_cnt);
331 }
332
333 static void InternetReadFile_test(int flags, const test_data_t *test)
334 {
335     char *post_data = NULL;
336     BOOL res, on_async = TRUE;
337     CHAR buffer[4000];
338     DWORD length, post_len = 0;
339     DWORD out;
340     const char *types[2] = { "*", NULL };
341     HINTERNET hi, hic = 0, hor = 0;
342
343     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
344
345     trace("Starting InternetReadFile test with flags 0x%x on url %s\n",flags,test->url);
346
347     trace("InternetOpenA <--\n");
348     hi = InternetOpenA((test->flags & TESTF_COMPRESSED) ? "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" : "",
349             INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
350     ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
351     trace("InternetOpenA -->\n");
352
353     if (hi == 0x0) goto abort;
354
355     pInternetSetStatusCallbackA(hi,&callback);
356
357     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
358
359     trace("InternetConnectA <--\n");
360     hic=InternetConnectA(hi, test->host, INTERNET_INVALID_PORT_NUMBER,
361                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
362     ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
363     trace("InternetConnectA -->\n");
364
365     if (hic == 0x0) goto abort;
366
367     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
368     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
369
370     trace("HttpOpenRequestA <--\n");
371     hor = HttpOpenRequestA(hic, test->post_data ? "POST" : "GET", test->path, NULL, NULL, types,
372                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
373                            0xdeadbead);
374     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
375         /*
376          * If the internet name can't be resolved we are probably behind
377          * a firewall or in some other way not directly connected to the
378          * Internet. Not enough reason to fail the test. Just ignore and
379          * abort.
380          */
381     } else  {
382         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
383     }
384     trace("HttpOpenRequestA -->\n");
385
386     if (hor == 0x0) goto abort;
387
388     length = sizeof(buffer);
389     res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
390     ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
391     ok(!strcmp(buffer, test->url), "Wrong URL %s, expected %s\n", buffer, test->url);
392
393     length = sizeof(buffer);
394     res = HttpQueryInfoA(hor, HTTP_QUERY_RAW_HEADERS, buffer, &length, 0x0);
395     ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
396     ok(length == 0, "HTTP_QUERY_RAW_HEADERS: expected length 0, but got %d\n", length);
397     ok(!strcmp(buffer, ""), "HTTP_QUERY_RAW_HEADERS: expected string \"\", but got \"%s\"\n", buffer);
398
399     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
400     CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
401     CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
402     if(test->flags & TESTF_ALLOW_COOKIE) {
403         SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
404         SET_OPTIONAL(INTERNET_STATUS_COOKIE_RECEIVED);
405     }
406     if (first_connection_to_test_url)
407     {
408         SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
409         SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
410     }
411     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
412     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
413     SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, (test->flags & TESTF_REDIRECT) ? 2 : 1);
414     SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, (test->flags & TESTF_REDIRECT) ? 2 : 1);
415     SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, (test->flags & TESTF_REDIRECT) ? 2 : 1);
416     SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, (test->flags & TESTF_REDIRECT) ? 2 : 1);
417     if(test->flags & TESTF_REDIRECT) {
418         SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
419         SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
420     }
421     SET_EXPECT(INTERNET_STATUS_REDIRECT);
422     SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
423     SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
424     if (flags & INTERNET_FLAG_ASYNC)
425         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
426
427     if(test->flags & TESTF_COMPRESSED) {
428         BOOL b = TRUE;
429
430         res = InternetSetOption(hor, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b));
431         ok(res || broken(!res && GetLastError() == ERROR_INTERNET_INVALID_OPTION),
432            "InternetSetOption failed: %u\n", GetLastError());
433         if(!res)
434             goto abort;
435     }
436
437     trace("HttpSendRequestA -->\n");
438     if(test->post_data) {
439         post_len = strlen(test->post_data);
440         post_data = HeapAlloc(GetProcessHeap(), 0, post_len);
441         memcpy(post_data, test->post_data, post_len);
442     }
443     SetLastError(0xdeadbeef);
444     res = HttpSendRequestA(hor, test->headers, -1, post_data, post_len);
445     if (flags & INTERNET_FLAG_ASYNC)
446         ok(!res && (GetLastError() == ERROR_IO_PENDING),
447             "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
448     else
449         ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
450            "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
451     trace("HttpSendRequestA <--\n");
452
453     if (flags & INTERNET_FLAG_ASYNC) {
454         WaitForSingleObject(hCompleteEvent, INFINITE);
455         ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
456     }
457     HeapFree(GetProcessHeap(), 0, post_data);
458
459     if(test->flags & TESTF_ALLOW_COOKIE) {
460         CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
461         CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_RECEIVED);
462     }
463     if (first_connection_to_test_url)
464     {
465         if (! proxy_active())
466         {
467             CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
468             CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
469         }
470         else
471         {
472             CLEAR_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
473             CLEAR_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
474         }
475     }
476     else
477     {
478         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
479         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
480     }
481     CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, (test->flags & TESTF_REDIRECT) ? 2 : 1);
482     CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, (test->flags & TESTF_REDIRECT) ? 2 : 1);
483     CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, (test->flags & TESTF_REDIRECT) ? 2 : 1);
484     CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, (test->flags & TESTF_REDIRECT) ? 2 : 1);
485     if(test->flags & TESTF_REDIRECT)
486         CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
487     if (flags & INTERNET_FLAG_ASYNC)
488         CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
489     /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
490     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
491     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
492
493     length = 4;
494     res = InternetQueryOptionA(hor,INTERNET_OPTION_REQUEST_FLAGS,&out,&length);
495     ok(res, "InternetQueryOptionA(INTERNET_OPTION_REQUEST) failed with error %d\n", GetLastError());
496
497     length = 100;
498     res = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
499     ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed with error %d\n", GetLastError());
500
501     length = sizeof(buffer);
502     res = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
503     ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
504     buffer[length]=0;
505
506     length = sizeof(buffer);
507     res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
508     ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
509     ok(!strcmp(buffer, test->redirected_url), "Wrong URL %s\n", buffer);
510
511     length = 16;
512     res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
513     trace("Option HTTP_QUERY_CONTENT_LENGTH -> %i  %s  (%u)\n",res,buffer,GetLastError());
514     if(test->flags & TESTF_COMPRESSED)
515         ok(!res && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
516            "expected ERROR_HTTP_HEADER_NOT_FOUND, got %x (%u)\n", res, GetLastError());
517
518     length = 100;
519     res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
520     buffer[length]=0;
521     trace("Option HTTP_QUERY_CONTENT_TYPE -> %i  %s\n",res,buffer);
522
523     length = 100;
524     res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_ENCODING,buffer,&length,0x0);
525     buffer[length]=0;
526     trace("Option HTTP_QUERY_CONTENT_ENCODING -> %i  %s\n",res,buffer);
527
528     SetLastError(0xdeadbeef);
529     res = InternetReadFile(NULL, buffer, 100, &length);
530     ok(!res, "InternetReadFile should have failed\n");
531     ok(GetLastError() == ERROR_INVALID_HANDLE,
532         "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
533         GetLastError());
534
535     length = 100;
536     trace("Entering Query loop\n");
537
538     while (TRUE)
539     {
540         if (flags & INTERNET_FLAG_ASYNC)
541             SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
542         res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
543         if (flags & INTERNET_FLAG_ASYNC)
544         {
545             if (res)
546             {
547                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
548             }
549             else if (GetLastError() == ERROR_IO_PENDING)
550             {
551                 trace("PENDING\n");
552                 /* on some tests, InternetQueryDataAvailable returns non-zero length and ERROR_IO_PENDING */
553                 if(!(test->flags & TESTF_CHUNKED))
554                     ok(!length, "InternetQueryDataAvailable returned ERROR_IO_PENDING and %u length\n", length);
555                 WaitForSingleObject(hCompleteEvent, INFINITE);
556                 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
557                 ok(req_error, "req_error = 0\n");
558                 continue;
559             }else {
560                 ok(0, "InternetQueryDataAvailable failed: %u\n", GetLastError());
561             }
562         }else {
563             ok(res, "InternetQueryDataAvailable failed: %u\n", GetLastError());
564         }
565         trace("LENGTH %d\n", length);
566         if(test->flags & TESTF_CHUNKED)
567             ok(length <= 8192, "length = %d, expected <= 8192\n", length);
568         if (length)
569         {
570             char *buffer;
571             buffer = HeapAlloc(GetProcessHeap(),0,length+1);
572
573             res = InternetReadFile(hor,buffer,length,&length);
574
575             buffer[length]=0;
576
577             trace("ReadFile -> %s %i\n",res?"TRUE":"FALSE",length);
578
579             if(test->content)
580                 ok(!strcmp(buffer, test->content), "buffer = '%s', expected '%s'\n", buffer, test->content);
581             HeapFree(GetProcessHeap(),0,buffer);
582         }else {
583             ok(!on_async, "Returned zero size in response to request complete\n");
584             break;
585         }
586         on_async = FALSE;
587     }
588     if(test->flags & TESTF_REDIRECT) {
589         CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
590         CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
591     }
592 abort:
593     trace("aborting\n");
594     close_async_handle(hi, hCompleteEvent, 2);
595     CloseHandle(hCompleteEvent);
596     first_connection_to_test_url = FALSE;
597 }
598
599 static void InternetReadFile_chunked_test(void)
600 {
601     BOOL res;
602     CHAR buffer[4000];
603     DWORD length;
604     const char *types[2] = { "*", NULL };
605     HINTERNET hi, hic = 0, hor = 0;
606
607     trace("Starting InternetReadFile chunked test\n");
608
609     trace("InternetOpenA <--\n");
610     hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
611     ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
612     trace("InternetOpenA -->\n");
613
614     if (hi == 0x0) goto abort;
615
616     trace("InternetConnectA <--\n");
617     hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
618                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
619     ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
620     trace("InternetConnectA -->\n");
621
622     if (hic == 0x0) goto abort;
623
624     trace("HttpOpenRequestA <--\n");
625     hor = HttpOpenRequestA(hic, "GET", "/tests/chunked", NULL, NULL, types,
626                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
627                            0xdeadbead);
628     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
629         /*
630          * If the internet name can't be resolved we are probably behind
631          * a firewall or in some other way not directly connected to the
632          * Internet. Not enough reason to fail the test. Just ignore and
633          * abort.
634          */
635     } else  {
636         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
637     }
638     trace("HttpOpenRequestA -->\n");
639
640     if (hor == 0x0) goto abort;
641
642     trace("HttpSendRequestA -->\n");
643     SetLastError(0xdeadbeef);
644     res = HttpSendRequestA(hor, "", -1, NULL, 0);
645     ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
646        "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
647     trace("HttpSendRequestA <--\n");
648
649     length = 100;
650     res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
651     buffer[length]=0;
652     trace("Option CONTENT_TYPE -> %i  %s\n",res,buffer);
653
654     SetLastError( 0xdeadbeef );
655     length = 100;
656     res = HttpQueryInfoA(hor,HTTP_QUERY_TRANSFER_ENCODING,buffer,&length,0x0);
657     buffer[length]=0;
658     trace("Option TRANSFER_ENCODING -> %i  %s\n",res,buffer);
659     ok( res || ( proxy_active() && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND ),
660         "Failed to get TRANSFER_ENCODING option, error %u\n", GetLastError() );
661     ok( !strcmp( buffer, "chunked" ) || ( ! res && proxy_active() && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND ),
662         "Wrong transfer encoding '%s'\n", buffer );
663
664     SetLastError( 0xdeadbeef );
665     length = 16;
666     res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
667     ok( !res, "Found CONTENT_LENGTH option '%s'\n", buffer );
668     ok( GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "Wrong error %u\n", GetLastError() );
669
670     length = 100;
671     trace("Entering Query loop\n");
672
673     while (TRUE)
674     {
675         res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
676         ok(!(!res && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
677         ok(res, "InternetQueryDataAvailable failed, error %d\n", GetLastError());
678         trace("got %u available\n",length);
679         if (length)
680         {
681             DWORD got;
682             char *buffer = HeapAlloc(GetProcessHeap(),0,length+1);
683
684             res = InternetReadFile(hor,buffer,length,&got);
685
686             buffer[got]=0;
687             trace("ReadFile -> %i %i\n",res,got);
688             ok( length == got, "only got %u of %u available\n", got, length );
689             ok( buffer[got-1] == '\n', "received partial line '%s'\n", buffer );
690
691             HeapFree(GetProcessHeap(),0,buffer);
692             if (!got) break;
693         }
694         if (length == 0)
695             break;
696     }
697 abort:
698     trace("aborting\n");
699     if (hor != 0x0) {
700         res = InternetCloseHandle(hor);
701         ok (res, "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
702     }
703     if (hi != 0x0) {
704         res = InternetCloseHandle(hi);
705         ok (res, "InternetCloseHandle of handle opened by InternetOpenA failed\n");
706     }
707 }
708
709 static void InternetReadFileExA_test(int flags)
710 {
711     DWORD rc;
712     DWORD length;
713     const char *types[2] = { "*", NULL };
714     HINTERNET hi, hic = 0, hor = 0;
715     INTERNET_BUFFERS inetbuffers;
716
717     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
718
719     trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
720
721     trace("InternetOpenA <--\n");
722     hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
723     ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
724     trace("InternetOpenA -->\n");
725
726     if (hi == 0x0) goto abort;
727
728     pInternetSetStatusCallbackA(hi,&callback);
729
730     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
731
732     trace("InternetConnectA <--\n");
733     hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
734                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
735     ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
736     trace("InternetConnectA -->\n");
737
738     if (hic == 0x0) goto abort;
739
740     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
741     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
742
743     trace("HttpOpenRequestA <--\n");
744     hor = HttpOpenRequestA(hic, "GET", "/tests/redirect", NULL, NULL, types,
745                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
746                            0xdeadbead);
747     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
748         /*
749          * If the internet name can't be resolved we are probably behind
750          * a firewall or in some other way not directly connected to the
751          * Internet. Not enough reason to fail the test. Just ignore and
752          * abort.
753          */
754     } else  {
755         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
756     }
757     trace("HttpOpenRequestA -->\n");
758
759     if (hor == 0x0) goto abort;
760
761     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
762     CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
763     CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
764     if (first_connection_to_test_url)
765     {
766         SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
767         SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
768     }
769     SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
770     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
771     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
772     SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
773     SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
774     SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
775     SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
776     SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
777     SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
778     SET_EXPECT(INTERNET_STATUS_REDIRECT);
779     SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
780     SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
781     if (flags & INTERNET_FLAG_ASYNC)
782         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
783     else
784         SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
785
786     trace("HttpSendRequestA -->\n");
787     SetLastError(0xdeadbeef);
788     rc = HttpSendRequestA(hor, "", -1, NULL, 0);
789     if (flags & INTERNET_FLAG_ASYNC)
790         ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
791             "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
792     else
793         ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
794            "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
795     trace("HttpSendRequestA <--\n");
796
797     if (!rc && (GetLastError() == ERROR_IO_PENDING)) {
798         WaitForSingleObject(hCompleteEvent, INFINITE);
799         ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
800     }
801
802     if (first_connection_to_test_url)
803     {
804         CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
805         CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
806     }
807     else
808     {
809         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
810         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
811     }
812     CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
813     CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
814     CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
815     CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
816     CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
817     CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
818     CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
819     if (flags & INTERNET_FLAG_ASYNC)
820         CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
821     else
822         todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
823     CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
824     /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
825     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
826     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
827
828     /* tests invalid dwStructSize */
829     inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS)+1;
830     inetbuffers.lpcszHeader = NULL;
831     inetbuffers.dwHeadersLength = 0;
832     inetbuffers.dwBufferLength = 10;
833     inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
834     inetbuffers.dwOffsetHigh = 1234;
835     inetbuffers.dwOffsetLow = 5678;
836     rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
837     ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
838         "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
839         rc ? "TRUE" : "FALSE", GetLastError());
840     HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
841
842     /* tests to see whether lpcszHeader is used - it isn't */
843     inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
844     inetbuffers.lpcszHeader = (LPCTSTR)0xdeadbeef;
845     inetbuffers.dwHeadersLength = 255;
846     inetbuffers.dwBufferLength = 0;
847     inetbuffers.lpvBuffer = NULL;
848     inetbuffers.dwOffsetHigh = 1234;
849     inetbuffers.dwOffsetLow = 5678;
850     SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
851     SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
852     rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
853     ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
854         trace("read %i bytes\n", inetbuffers.dwBufferLength);
855     todo_wine
856     {
857         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
858         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
859     }
860
861     rc = InternetReadFileEx(NULL, &inetbuffers, 0, 0xdeadcafe);
862     ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
863         "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
864         rc ? "TRUE" : "FALSE", GetLastError());
865
866     length = 0;
867     trace("Entering Query loop\n");
868
869     while (TRUE)
870     {
871         inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
872         inetbuffers.dwBufferLength = 1024;
873         inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
874         inetbuffers.dwOffsetHigh = 1234;
875         inetbuffers.dwOffsetLow = 5678;
876
877         SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
878         SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
879         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
880         rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
881         if (!rc)
882         {
883             if (GetLastError() == ERROR_IO_PENDING)
884             {
885                 trace("InternetReadFileEx -> PENDING\n");
886                 ok(flags & INTERNET_FLAG_ASYNC,
887                    "Should not get ERROR_IO_PENDING without INTERNET_FLAG_ASYNC\n");
888                 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
889                 WaitForSingleObject(hCompleteEvent, INFINITE);
890                 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
891                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
892                 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
893             }
894             else
895             {
896                 trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
897                 break;
898             }
899         }
900         else
901         {
902             trace("InternetReadFileEx -> SUCCEEDED\n");
903             CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
904             if (inetbuffers.dwBufferLength)
905             {
906                 todo_wine {
907                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
908                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
909                 }
910             }
911             else
912             {
913                 /* Win98 still sends these when 0 bytes are read, WinXP does not */
914                 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
915                 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
916             }
917         }
918
919         trace("read %i bytes\n", inetbuffers.dwBufferLength);
920         ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
921
922         ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
923             "InternetReadFileEx sets offsets to 0x%x%08x\n",
924             inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
925
926         HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
927
928         if (!inetbuffers.dwBufferLength)
929             break;
930
931         length += inetbuffers.dwBufferLength;
932     }
933     ok(length > 0, "failed to read any of the document\n");
934     trace("Finished. Read %d bytes\n", length);
935
936 abort:
937     close_async_handle(hi, hCompleteEvent, 2);
938     CloseHandle(hCompleteEvent);
939     first_connection_to_test_url = FALSE;
940 }
941
942 static void InternetOpenUrlA_test(void)
943 {
944   HINTERNET myhinternet, myhttp;
945   char buffer[0x400];
946   DWORD size, readbytes, totalbytes=0;
947   BOOL ret;
948
949   myhinternet = InternetOpen("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
950   ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
951   size = 0x400;
952   ret = InternetCanonicalizeUrl(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
953   ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
954
955   SetLastError(0);
956   myhttp = InternetOpenUrl(myhinternet, TEST_URL, 0, 0,
957                            INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
958   if (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
959     return; /* WinXP returns this when not connected to the net */
960   ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
961   ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
962   ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
963   totalbytes += readbytes;
964   while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
965     totalbytes += readbytes;
966   trace("read 0x%08x bytes\n",totalbytes);
967
968   InternetCloseHandle(myhttp);
969   InternetCloseHandle(myhinternet);
970 }
971
972 static void HttpSendRequestEx_test(void)
973 {
974     HINTERNET hSession;
975     HINTERNET hConnect;
976     HINTERNET hRequest;
977
978     INTERNET_BUFFERS BufferIn;
979     DWORD dwBytesWritten, dwBytesRead, error;
980     CHAR szBuffer[256];
981     int i;
982     BOOL ret;
983
984     static char szPostData[] = "mode=Test";
985     static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
986
987     hSession = InternetOpen("Wine Regression Test",
988             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
989     ok( hSession != NULL ,"Unable to open Internet session\n");
990     hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
991             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
992             0);
993     ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
994     hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
995             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
996     if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
997     {
998         skip( "Network unreachable, skipping test\n" );
999         goto done;
1000     }
1001     ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
1002
1003
1004     BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS);
1005     BufferIn.Next = (LPINTERNET_BUFFERS)0xdeadcab;
1006     BufferIn.lpcszHeader = szContentType;
1007     BufferIn.dwHeadersLength = sizeof(szContentType)-1;
1008     BufferIn.dwHeadersTotal = sizeof(szContentType)-1;
1009     BufferIn.lpvBuffer = szPostData;
1010     BufferIn.dwBufferLength = 3;
1011     BufferIn.dwBufferTotal = sizeof(szPostData)-1;
1012     BufferIn.dwOffsetLow = 0;
1013     BufferIn.dwOffsetHigh = 0;
1014
1015     SetLastError(0xdeadbeef);
1016     ret = HttpSendRequestEx(hRequest, &BufferIn, NULL, 0 ,0);
1017     error = GetLastError();
1018     ok(ret, "HttpSendRequestEx Failed with error %u\n", error);
1019     ok(error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %u\n", error);
1020
1021     for (i = 3; szPostData[i]; i++)
1022         ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
1023                 "InternetWriteFile failed\n");
1024
1025     ok(HttpEndRequest(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
1026
1027     ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
1028             "Unable to read response\n");
1029     szBuffer[dwBytesRead] = 0;
1030
1031     ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
1032     ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0 || broken(proxy_active()),"Got string %s\n",szBuffer);
1033
1034     ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1035 done:
1036     ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1037     ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1038 }
1039
1040 static void InternetOpenRequest_test(void)
1041 {
1042     HINTERNET session, connect, request;
1043     static const char *types[] = { "*", "", NULL };
1044     static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
1045     static const WCHAR *typesW[] = { any, empty, NULL };
1046     BOOL ret;
1047
1048     session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1049     ok(session != NULL ,"Unable to open Internet session\n");
1050
1051     connect = InternetConnectA(session, NULL, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1052                               INTERNET_SERVICE_HTTP, 0, 0);
1053     ok(connect == NULL, "InternetConnectA should have failed\n");
1054     ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
1055
1056     connect = InternetConnectA(session, "", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1057                               INTERNET_SERVICE_HTTP, 0, 0);
1058     ok(connect == NULL, "InternetConnectA should have failed\n");
1059     ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
1060
1061     connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1062                               INTERNET_SERVICE_HTTP, 0, 0);
1063     ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1064
1065     request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1066     if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1067     {
1068         skip( "Network unreachable, skipping test\n" );
1069         goto done;
1070     }
1071     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1072
1073     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1074     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1075     ok(InternetCloseHandle(request), "Close request handle failed\n");
1076
1077     request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1078     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1079
1080     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1081     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1082     ok(InternetCloseHandle(request), "Close request handle failed\n");
1083
1084 done:
1085     ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1086     ok(InternetCloseHandle(session), "Close session handle failed\n");
1087 }
1088
1089 static void test_http_cache(void)
1090 {
1091     HINTERNET session, connect, request;
1092     char file_name[MAX_PATH], url[INTERNET_MAX_URL_LENGTH];
1093     DWORD size, file_size;
1094     BYTE buf[100];
1095     HANDLE file;
1096     BOOL ret;
1097
1098     static const char *types[] = { "*", "", NULL };
1099
1100     session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1101     ok(session != NULL ,"Unable to open Internet session\n");
1102
1103     connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1104                               INTERNET_SERVICE_HTTP, 0, 0);
1105     ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1106
1107     request = HttpOpenRequestA(connect, NULL, "/tests/hello.html", NULL, NULL, types, INTERNET_FLAG_NEED_FILE, 0);
1108     if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1109     {
1110         skip( "Network unreachable, skipping test\n" );
1111
1112         ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1113         ok(InternetCloseHandle(session), "Close session handle failed\n");
1114
1115         return;
1116     }
1117     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1118
1119     size = sizeof(url);
1120     ret = InternetQueryOptionA(request, INTERNET_OPTION_URL, url, &size);
1121     ok(ret, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
1122     ok(!strcmp(url, "http://test.winehq.org/tests/hello.html"), "Wrong URL %s\n", url);
1123
1124     size = sizeof(file_name);
1125     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1126     ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1127     ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1128     ok(!size, "size = %d\n", size);
1129
1130     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1131     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1132
1133     size = sizeof(file_name);
1134     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1135     ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1136
1137     file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1138                       FILE_ATTRIBUTE_NORMAL, NULL);
1139     ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1140     file_size = GetFileSize(file, NULL);
1141     todo_wine ok(file_size == 106, "file size = %u\n", file_size);
1142
1143     size = sizeof(buf);
1144     ret = InternetReadFile(request, buf, sizeof(buf), &size);
1145     ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1146     ok(size == 100, "size = %u\n", size);
1147
1148     file_size = GetFileSize(file, NULL);
1149     todo_wine ok(file_size == 106, "file size = %u\n", file_size);
1150     CloseHandle(file);
1151
1152     ok(InternetCloseHandle(request), "Close request handle failed\n");
1153
1154     file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1155                       FILE_ATTRIBUTE_NORMAL, NULL);
1156     ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1157     CloseHandle(file);
1158
1159     /* Send the same request, requiring it to be retrieved from the cache */
1160     request = HttpOpenRequest(connect, "GET", "/tests/hello.html", NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
1161
1162     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1163     ok(ret, "HttpSendRequest failed\n");
1164
1165     size = sizeof(buf);
1166     ret = InternetReadFile(request, buf, sizeof(buf), &size);
1167     ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1168     ok(size == 100, "size = %u\n", size);
1169
1170     ok(InternetCloseHandle(request), "Close request handle failed\n");
1171
1172     request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1173     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1174
1175     size = sizeof(file_name);
1176     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1177     ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1178     ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1179     ok(!size, "size = %d\n", size);
1180
1181     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1182     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1183
1184     size = sizeof(file_name);
1185     file_name[0] = 0;
1186     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1187     if (ret)
1188     {
1189         file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1190                       FILE_ATTRIBUTE_NORMAL, NULL);
1191         ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1192         CloseHandle(file);
1193     }
1194     else
1195     {
1196         /* < IE8 */
1197         ok(file_name[0] == 0, "Didn't expect a file name\n");
1198     }
1199
1200     ok(InternetCloseHandle(request), "Close request handle failed\n");
1201     ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1202     ok(InternetCloseHandle(session), "Close session handle failed\n");
1203 }
1204
1205 static void HttpHeaders_test(void)
1206 {
1207     HINTERNET hSession;
1208     HINTERNET hConnect;
1209     HINTERNET hRequest;
1210     CHAR      buffer[256];
1211     WCHAR     wbuffer[256];
1212     DWORD     len = 256;
1213     DWORD     oldlen;
1214     DWORD     index = 0;
1215
1216     hSession = InternetOpen("Wine Regression Test",
1217             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1218     ok( hSession != NULL ,"Unable to open Internet session\n");
1219     hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
1220             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1221             0);
1222     ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
1223     hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
1224             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1225     if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1226     {
1227         skip( "Network unreachable, skipping test\n" );
1228         goto done;
1229     }
1230     ok( hRequest != NULL, "Failed to open request handle\n");
1231
1232     index = 0;
1233     len = sizeof(buffer);
1234     strcpy(buffer,"Warning");
1235     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1236                buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1237
1238     ok(HttpAddRequestHeaders(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1239             "Failed to add new header\n");
1240
1241     index = 0;
1242     len = sizeof(buffer);
1243     strcpy(buffer,"Warning");
1244     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1245                 buffer,&len,&index),"Unable to query header\n");
1246     ok(index == 1, "Index was not incremented\n");
1247     ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1248     ok(len == 5, "Invalid length (exp. 5, got %d)\n", len);
1249     ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1250     len = sizeof(buffer);
1251     strcpy(buffer,"Warning");
1252     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1253                 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1254
1255     index = 0;
1256     len = 5; /* could store the string but not the NULL terminator */
1257     strcpy(buffer,"Warning");
1258     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1259                 buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1260     ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1261     ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
1262
1263     /* a call with NULL will fail but will return the length */
1264     index = 0;
1265     len = sizeof(buffer);
1266     SetLastError(0xdeadbeef);
1267     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1268                 NULL,&len,&index) == FALSE,"Query worked\n");
1269     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1270     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1271     ok(index == 0, "Index was incremented\n");
1272
1273     /* even for a len that is too small */
1274     index = 0;
1275     len = 15;
1276     SetLastError(0xdeadbeef);
1277     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1278                 NULL,&len,&index) == FALSE,"Query worked\n");
1279     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1280     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1281     ok(index == 0, "Index was incremented\n");
1282
1283     index = 0;
1284     len = 0;
1285     SetLastError(0xdeadbeef);
1286     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1287                 NULL,&len,&index) == FALSE,"Query worked\n");
1288     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1289     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1290     ok(index == 0, "Index was incremented\n");
1291     oldlen = len;   /* bytes; at least long enough to hold buffer & nul */
1292
1293
1294     /* a working query */
1295     index = 0;
1296     len = sizeof(buffer);
1297     memset(buffer, 'x', sizeof(buffer));
1298     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1299                 buffer,&len,&index),"Unable to query header\n");
1300     ok(len + sizeof(CHAR) <= oldlen, "Result longer than advertised\n");
1301     ok((len < sizeof(buffer)-sizeof(CHAR)) && (buffer[len/sizeof(CHAR)] == 0),"No NUL at end\n");
1302     ok(len == strlen(buffer) * sizeof(CHAR), "Length wrong\n");
1303     /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1304     ok(strncmp(buffer, "POST /posttest.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1305     ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1306     ok(index == 0, "Index was incremented\n");
1307
1308     /* Like above two tests, but for W version */
1309
1310     index = 0;
1311     len = 0;
1312     SetLastError(0xdeadbeef);
1313     ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1314                 NULL,&len,&index) == FALSE,"Query worked\n");
1315     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1316     ok(len > 80, "Invalid length (exp. more than 80, got %d)\n", len);
1317     ok(index == 0, "Index was incremented\n");
1318     oldlen = len;   /* bytes; at least long enough to hold buffer & nul */
1319
1320     /* a working query */
1321     index = 0;
1322     len = sizeof(wbuffer);
1323     memset(wbuffer, 'x', sizeof(wbuffer));
1324     ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1325                 wbuffer,&len,&index),"Unable to query header\n");
1326     ok(len + sizeof(WCHAR) <= oldlen, "Result longer than advertised\n");
1327     ok(len == lstrlenW(wbuffer) * sizeof(WCHAR), "Length wrong\n");
1328     ok((len < sizeof(wbuffer)-sizeof(WCHAR)) && (wbuffer[len/sizeof(WCHAR)] == 0),"No NUL at end\n");
1329     ok(index == 0, "Index was incremented\n");
1330
1331     /* end of W version tests */
1332
1333     /* Without HTTP_QUERY_FLAG_REQUEST_HEADERS */
1334     index = 0;
1335     len = sizeof(buffer);
1336     memset(buffer, 'x', sizeof(buffer));
1337     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF,
1338                 buffer,&len,&index) == TRUE,"Query failed\n");
1339     ok(len == 2, "Expected 2, got %d\n", len);
1340     ok(strcmp(buffer, "\r\n") == 0, "Expected CRLF, got '%s'\n", buffer);
1341     ok(index == 0, "Index was incremented\n");
1342
1343     ok(HttpAddRequestHeaders(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1344             "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1345
1346     index = 0;
1347     len = sizeof(buffer);
1348     strcpy(buffer,"Warning");
1349     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1350                 buffer,&len,&index),"Unable to query header\n");
1351     ok(index == 1, "Index was not incremented\n");
1352     ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1353     len = sizeof(buffer);
1354     strcpy(buffer,"Warning");
1355     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1356                 buffer,&len,&index),"Failed to get second header\n");
1357     ok(index == 2, "Index was not incremented\n");
1358     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1359     len = sizeof(buffer);
1360     strcpy(buffer,"Warning");
1361     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1362                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1363
1364     ok(HttpAddRequestHeaders(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1365
1366     index = 0;
1367     len = sizeof(buffer);
1368     strcpy(buffer,"Warning");
1369     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1370                 buffer,&len,&index),"Unable to query header\n");
1371     ok(index == 1, "Index was not incremented\n");
1372     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1373     len = sizeof(buffer);
1374     strcpy(buffer,"Warning");
1375     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1376                 buffer,&len,&index),"Failed to get second header\n");
1377     ok(index == 2, "Index was not incremented\n");
1378     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1379     len = sizeof(buffer);
1380     strcpy(buffer,"Warning");
1381     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1382                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1383
1384     ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1385
1386     index = 0;
1387     len = sizeof(buffer);
1388     strcpy(buffer,"Warning");
1389     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1390                 buffer,&len,&index),"Unable to query header\n");
1391     ok(index == 1, "Index was not incremented\n");
1392     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1393     len = sizeof(buffer);
1394     strcpy(buffer,"Warning");
1395     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1396                 buffer,&len,&index),"Failed to get second header\n");
1397     ok(index == 2, "Index was not incremented\n");
1398     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1399     len = sizeof(buffer);
1400     strcpy(buffer,"Warning");
1401     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1402                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1403
1404     ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1405
1406     index = 0;
1407     len = sizeof(buffer);
1408     strcpy(buffer,"Warning");
1409     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1410                 buffer,&len,&index),"Unable to query header\n");
1411     ok(index == 1, "Index was not incremented\n");
1412     ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
1413     len = sizeof(buffer);
1414     strcpy(buffer,"Warning");
1415     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1416     ok(index == 2, "Index was not incremented\n");
1417     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1418     len = sizeof(buffer);
1419     strcpy(buffer,"Warning");
1420     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1421
1422     ok(HttpAddRequestHeaders(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1423
1424     index = 0;
1425     len = sizeof(buffer);
1426     strcpy(buffer,"Warning");
1427     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1428     ok(index == 1, "Index was not incremented\n");
1429     ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
1430     len = sizeof(buffer);
1431     strcpy(buffer,"Warning");
1432     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1433     ok(index == 2, "Index was not incremented\n");
1434     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1435     len = sizeof(buffer);
1436     strcpy(buffer,"Warning");
1437     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1438
1439     ok(HttpAddRequestHeaders(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1440
1441     index = 0;
1442     len = sizeof(buffer);
1443     strcpy(buffer,"Warning");
1444     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1445     ok(index == 1, "Index was not incremented\n");
1446     ok(strcmp(buffer,"test2, test4, test5; test6")==0, "incorrect string was returned(%s)\n",buffer);
1447     len = sizeof(buffer);
1448     strcpy(buffer,"Warning");
1449     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1450     ok(index == 2, "Index was not incremented\n");
1451     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1452     len = sizeof(buffer);
1453     strcpy(buffer,"Warning");
1454     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1455
1456     ok(HttpAddRequestHeaders(hRequest,"Warning:test7",-1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE), "HTTP_ADDREQ_FLAG_ADD with HTTP_ADDREQ_FLAG_REPALCE Did not work\n");
1457
1458     index = 0;
1459     len = sizeof(buffer);
1460     strcpy(buffer,"Warning");
1461     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1462     ok(index == 1, "Index was not incremented\n");
1463     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1464     len = sizeof(buffer);
1465     strcpy(buffer,"Warning");
1466     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1467     ok(index == 2, "Index was not incremented\n");
1468     ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
1469     len = sizeof(buffer);
1470     strcpy(buffer,"Warning");
1471     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1472
1473     /* Ensure that blank headers are ignored and don't cause a failure */
1474     ok(HttpAddRequestHeaders(hRequest,"\r\nBlankTest:value\r\n\r\n",-1, HTTP_ADDREQ_FLAG_ADD_IF_NEW), "Failed to add header with blank entries in list\n");
1475
1476     index = 0;
1477     len = sizeof(buffer);
1478     strcpy(buffer,"BlankTest");
1479     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1480     ok(index == 1, "Index was not incremented\n");
1481     ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
1482
1483     /* Ensure that malformed header separators are ignored and don't cause a failure */
1484     ok(HttpAddRequestHeaders(hRequest,"\r\rMalformedTest:value\n\nMalformedTestTwo: value2\rMalformedTestThree: value3\n\n\r\r\n",-1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE),
1485         "Failed to add header with malformed entries in list\n");
1486
1487     index = 0;
1488     len = sizeof(buffer);
1489     strcpy(buffer,"MalformedTest");
1490     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1491     ok(index == 1, "Index was not incremented\n");
1492     ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
1493     index = 0;
1494     len = sizeof(buffer);
1495     strcpy(buffer,"MalformedTestTwo");
1496     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1497     ok(index == 1, "Index was not incremented\n");
1498     ok(strcmp(buffer,"value2")==0, "incorrect string was returned(%s)\n",buffer);
1499     index = 0;
1500     len = sizeof(buffer);
1501     strcpy(buffer,"MalformedTestThree");
1502     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1503     ok(index == 1, "Index was not incremented\n");
1504     ok(strcmp(buffer,"value3")==0, "incorrect string was returned(%s)\n",buffer);
1505
1506     ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1507 done:
1508     ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1509     ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1510 }
1511
1512 static const char garbagemsg[] =
1513 "Garbage: Header\r\n";
1514
1515 static const char contmsg[] =
1516 "HTTP/1.1 100 Continue\r\n";
1517
1518 static const char expandcontmsg[] =
1519 "HTTP/1.1 100 Continue\r\n"
1520 "Server: winecontinue\r\n"
1521 "Tag: something witty\r\n"
1522 "\r\n";
1523
1524 static const char okmsg[] =
1525 "HTTP/1.1 200 OK\r\n"
1526 "Server: winetest\r\n"
1527 "\r\n";
1528
1529 static const char okmsg2[] =
1530 "HTTP/1.1 200 OK\r\n"
1531 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
1532 "Server: winetest\r\n"
1533 "Content-Length: 0\r\n"
1534 "Set-Cookie: one\r\n"
1535 "Set-Cookie: two\r\n"
1536 "\r\n";
1537
1538 static const char notokmsg[] =
1539 "HTTP/1.1 400 Bad Request\r\n"
1540 "Server: winetest\r\n"
1541 "\r\n";
1542
1543 static const char noauthmsg[] =
1544 "HTTP/1.1 401 Unauthorized\r\n"
1545 "Server: winetest\r\n"
1546 "Connection: close\r\n"
1547 "WWW-Authenticate: Basic realm=\"placebo\"\r\n"
1548 "\r\n";
1549
1550 static const char noauthmsg2[] =
1551 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed\r\n"
1552 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"
1553 "\0d`0|6\n"
1554 "Server: winetest\r\n";
1555
1556 static const char proxymsg[] =
1557 "HTTP/1.1 407 Proxy Authentication Required\r\n"
1558 "Server: winetest\r\n"
1559 "Proxy-Connection: close\r\n"
1560 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
1561 "\r\n";
1562
1563 static const char page1[] =
1564 "<HTML>\r\n"
1565 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
1566 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
1567 "</HTML>\r\n\r\n";
1568
1569 struct server_info {
1570     HANDLE hEvent;
1571     int port;
1572     int num_testH_retrievals;
1573 };
1574
1575 static DWORD CALLBACK server_thread(LPVOID param)
1576 {
1577     struct server_info *si = param;
1578     int r, c, i, on;
1579     SOCKET s;
1580     struct sockaddr_in sa;
1581     char buffer[0x100];
1582     WSADATA wsaData;
1583     int last_request = 0;
1584     char host_header[22];
1585     static int test_b = 0;
1586
1587     WSAStartup(MAKEWORD(1,1), &wsaData);
1588
1589     s = socket(AF_INET, SOCK_STREAM, 0);
1590     if (s == INVALID_SOCKET)
1591         return 1;
1592
1593     on = 1;
1594     setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
1595
1596     memset(&sa, 0, sizeof sa);
1597     sa.sin_family = AF_INET;
1598     sa.sin_port = htons(si->port);
1599     sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
1600
1601     r = bind(s, (struct sockaddr*) &sa, sizeof sa);
1602     if (r<0)
1603         return 1;
1604
1605     listen(s, 0);
1606
1607     SetEvent(si->hEvent);
1608
1609     sprintf(host_header, "Host: localhost:%d", si->port);
1610
1611     do
1612     {
1613         c = accept(s, NULL, NULL);
1614
1615         memset(buffer, 0, sizeof buffer);
1616         for(i=0; i<(sizeof buffer-1); i++)
1617         {
1618             r = recv(c, &buffer[i], 1, 0);
1619             if (r != 1)
1620                 break;
1621             if (i<4) continue;
1622             if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
1623                 buffer[i-3] == '\r' && buffer[i-1] == '\r')
1624                 break;
1625         }
1626         if (strstr(buffer, "GET /test1"))
1627         {
1628             if (!strstr(buffer, "Content-Length: 0"))
1629             {
1630                 send(c, okmsg, sizeof okmsg-1, 0);
1631                 send(c, page1, sizeof page1-1, 0);
1632             }
1633             else
1634                 send(c, notokmsg, sizeof notokmsg-1, 0);
1635         }
1636         if (strstr(buffer, "/test2"))
1637         {
1638             if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
1639             {
1640                 send(c, okmsg, sizeof okmsg-1, 0);
1641                 send(c, page1, sizeof page1-1, 0);
1642             }
1643             else
1644                 send(c, proxymsg, sizeof proxymsg-1, 0);
1645         }
1646         if (strstr(buffer, "/test3"))
1647         {
1648             if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
1649                 send(c, okmsg, sizeof okmsg-1, 0);
1650             else
1651                 send(c, noauthmsg, sizeof noauthmsg-1, 0);
1652         }
1653         if (strstr(buffer, "/test4"))
1654         {
1655             if (strstr(buffer, "Connection: Close"))
1656                 send(c, okmsg, sizeof okmsg-1, 0);
1657             else
1658                 send(c, notokmsg, sizeof notokmsg-1, 0);
1659         }
1660         if (strstr(buffer, "POST /test5") ||
1661             strstr(buffer, "RPC_IN_DATA /test5") ||
1662             strstr(buffer, "RPC_OUT_DATA /test5"))
1663         {
1664             if (strstr(buffer, "Content-Length: 0"))
1665             {
1666                 send(c, okmsg, sizeof okmsg-1, 0);
1667                 send(c, page1, sizeof page1-1, 0);
1668             }
1669             else
1670                 send(c, notokmsg, sizeof notokmsg-1, 0);
1671         }
1672         if (strstr(buffer, "GET /test6"))
1673         {
1674             send(c, contmsg, sizeof contmsg-1, 0);
1675             send(c, contmsg, sizeof contmsg-1, 0);
1676             send(c, okmsg, sizeof okmsg-1, 0);
1677             send(c, page1, sizeof page1-1, 0);
1678         }
1679         if (strstr(buffer, "POST /test7"))
1680         {
1681             if (strstr(buffer, "Content-Length: 100"))
1682             {
1683                 send(c, okmsg, sizeof okmsg-1, 0);
1684                 send(c, page1, sizeof page1-1, 0);
1685             }
1686             else
1687                 send(c, notokmsg, sizeof notokmsg-1, 0);
1688         }
1689         if (strstr(buffer, "/test8"))
1690         {
1691             if (!strstr(buffer, "Connection: Close") &&
1692                  strstr(buffer, "Connection: Keep-Alive") &&
1693                 !strstr(buffer, "Cache-Control: no-cache") &&
1694                 !strstr(buffer, "Pragma: no-cache") &&
1695                  strstr(buffer, host_header))
1696                 send(c, okmsg, sizeof okmsg-1, 0);
1697             else
1698                 send(c, notokmsg, sizeof notokmsg-1, 0);
1699         }
1700         if (strstr(buffer, "/test9"))
1701         {
1702             if (!strstr(buffer, "Connection: Close") &&
1703                 !strstr(buffer, "Connection: Keep-Alive") &&
1704                 !strstr(buffer, "Cache-Control: no-cache") &&
1705                 !strstr(buffer, "Pragma: no-cache") &&
1706                  strstr(buffer, host_header))
1707                 send(c, okmsg, sizeof okmsg-1, 0);
1708             else
1709                 send(c, notokmsg, sizeof notokmsg-1, 0);
1710         }
1711         if (strstr(buffer, "/testA"))
1712         {
1713             if (!strstr(buffer, "Connection: Close") &&
1714                 !strstr(buffer, "Connection: Keep-Alive") &&
1715                 (strstr(buffer, "Cache-Control: no-cache") ||
1716                  strstr(buffer, "Pragma: no-cache")) &&
1717                  strstr(buffer, host_header))
1718                 send(c, okmsg, sizeof okmsg-1, 0);
1719             else
1720                 send(c, notokmsg, sizeof notokmsg-1, 0);
1721         }
1722         if (!test_b && strstr(buffer, "/testB HTTP/1.1"))
1723         {
1724             test_b = 1;
1725             send(c, okmsg, sizeof okmsg-1, 0);
1726             recvfrom(c, buffer, sizeof buffer, 0, NULL, NULL);
1727             send(c, okmsg, sizeof okmsg-1, 0);
1728         }
1729         if (strstr(buffer, "/testC"))
1730         {
1731             if (strstr(buffer, "Cookie: cookie=biscuit"))
1732                 send(c, okmsg, sizeof okmsg-1, 0);
1733             else
1734                 send(c, notokmsg, sizeof notokmsg-1, 0);
1735         }
1736         if (strstr(buffer, "/testD"))
1737         {
1738             send(c, okmsg2, sizeof okmsg2-1, 0);
1739         }
1740         if (strstr(buffer, "/testE"))
1741         {
1742             send(c, noauthmsg2, sizeof noauthmsg2-1, 0);
1743         }
1744         if (strstr(buffer, "GET /quit"))
1745         {
1746             send(c, okmsg, sizeof okmsg-1, 0);
1747             send(c, page1, sizeof page1-1, 0);
1748             last_request = 1;
1749         }
1750         if (strstr(buffer, "GET /testF"))
1751         {
1752             send(c, expandcontmsg, sizeof expandcontmsg-1, 0);
1753             send(c, garbagemsg, sizeof garbagemsg-1, 0);
1754             send(c, contmsg, sizeof contmsg-1, 0);
1755             send(c, garbagemsg, sizeof garbagemsg-1, 0);
1756             send(c, okmsg, sizeof okmsg-1, 0);
1757             send(c, page1, sizeof page1-1, 0);
1758         }
1759         if (strstr(buffer, "GET /testG"))
1760         {
1761             send(c, page1, sizeof page1-1, 0);
1762         }
1763         if (strstr(buffer, "GET /testH"))
1764         {
1765             si->num_testH_retrievals++;
1766             if (!strstr(buffer, "Content-Length: 0"))
1767             {
1768                 send(c, okmsg, sizeof okmsg-1, 0);
1769                 send(c, page1, sizeof page1-1, 0);
1770             }
1771             else
1772                 send(c, notokmsg, sizeof notokmsg-1, 0);
1773         }
1774         if (strstr(buffer, "GET /test_no_content"))
1775         {
1776             static const char nocontentmsg[] = "HTTP/1.1 204 No Content\r\nConnection: close\r\n\r\n";
1777             send(c, nocontentmsg, sizeof(nocontentmsg)-1, 0);
1778         }
1779         if (strstr(buffer, "GET /test_conn_close"))
1780         {
1781             static const char conn_close_response[] = "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nsome content";
1782             send(c, conn_close_response, sizeof(conn_close_response)-1, 0);
1783             WaitForSingleObject(conn_close_event, INFINITE);
1784             trace("closing connection\n");
1785         }
1786
1787         shutdown(c, 2);
1788         closesocket(c);
1789     } while (!last_request);
1790
1791     closesocket(s);
1792
1793     return 0;
1794 }
1795
1796 static void test_basic_request(int port, const char *verb, const char *url)
1797 {
1798     HINTERNET hi, hc, hr;
1799     DWORD r, count;
1800     char buffer[0x100];
1801
1802     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1803     ok(hi != NULL, "open failed\n");
1804
1805     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1806     ok(hc != NULL, "connect failed\n");
1807
1808     hr = HttpOpenRequest(hc, verb, url, NULL, NULL, NULL, 0, 0);
1809     ok(hr != NULL, "HttpOpenRequest failed\n");
1810
1811     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1812     ok(r, "HttpSendRequest failed\n");
1813
1814     count = 0;
1815     memset(buffer, 0, sizeof buffer);
1816     SetLastError(0xdeadbeef);
1817     r = InternetReadFile(hr, buffer, sizeof buffer, &count);
1818     ok(r, "InternetReadFile failed %u\n", GetLastError());
1819     ok(count == sizeof page1 - 1, "count was wrong\n");
1820     ok(!memcmp(buffer, page1, sizeof page1), "http data wrong, got: %s\n", buffer);
1821
1822     InternetCloseHandle(hr);
1823     InternetCloseHandle(hc);
1824     InternetCloseHandle(hi);
1825 }
1826
1827 static void test_last_error(int port)
1828 {
1829     HINTERNET hi, hc, hr;
1830     DWORD error;
1831     BOOL r;
1832
1833     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1834     ok(hi != NULL, "open failed\n");
1835
1836     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1837     ok(hc != NULL, "connect failed\n");
1838
1839     hr = HttpOpenRequest(hc, NULL, "/test1", NULL, NULL, NULL, 0, 0);
1840     ok(hr != NULL, "HttpOpenRequest failed\n");
1841
1842     SetLastError(0xdeadbeef);
1843     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1844     error = GetLastError();
1845     ok(r, "HttpSendRequest failed\n");
1846     ok(error == ERROR_SUCCESS || broken(error != ERROR_SUCCESS), "expected ERROR_SUCCESS, got %u\n", error);
1847
1848     InternetCloseHandle(hr);
1849     InternetCloseHandle(hc);
1850     InternetCloseHandle(hi);
1851 }
1852
1853 static void test_proxy_indirect(int port)
1854 {
1855     HINTERNET hi, hc, hr;
1856     DWORD r, sz, val;
1857     char buffer[0x40];
1858
1859     hi = InternetOpen(NULL, 0, NULL, NULL, 0);
1860     ok(hi != NULL, "open failed\n");
1861
1862     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1863     ok(hc != NULL, "connect failed\n");
1864
1865     hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1866     ok(hr != NULL, "HttpOpenRequest failed\n");
1867
1868     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1869     ok(r, "HttpSendRequest failed\n");
1870
1871     sz = sizeof buffer;
1872     r = HttpQueryInfo(hr, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
1873     ok(r || GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo failed: %d\n", GetLastError());
1874     if (!r)
1875     {
1876         skip("missing proxy header, not testing remaining proxy headers\n");
1877         goto out;
1878     }
1879     ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
1880
1881     sz = sizeof buffer;
1882     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1883     ok(r, "HttpQueryInfo failed\n");
1884     ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1885
1886     sz = sizeof val;
1887     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &val, &sz, NULL);
1888     ok(r, "HttpQueryInfo failed\n");
1889     ok(val == 407, "proxy code wrong\n");
1890
1891     sz = sizeof buffer;
1892     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
1893     ok(r, "HttpQueryInfo failed\n");
1894     ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
1895
1896     sz = sizeof buffer;
1897     r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &sz, NULL);
1898     ok(r, "HttpQueryInfo failed\n");
1899     ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
1900
1901     sz = sizeof buffer;
1902     r = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &sz, NULL);
1903     ok(r, "HttpQueryInfo failed\n");
1904     ok(!strcmp(buffer, "winetest"), "http server wrong\n");
1905
1906     sz = sizeof buffer;
1907     r = HttpQueryInfo(hr, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
1908     ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
1909     ok(r == FALSE, "HttpQueryInfo failed\n");
1910
1911 out:
1912     InternetCloseHandle(hr);
1913     InternetCloseHandle(hc);
1914     InternetCloseHandle(hi);
1915 }
1916
1917 static void test_proxy_direct(int port)
1918 {
1919     HINTERNET hi, hc, hr;
1920     DWORD r, sz;
1921     char buffer[0x40];
1922     static CHAR username[] = "mike",
1923                 password[] = "1101";
1924
1925     sprintf(buffer, "localhost:%d\n", port);
1926     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
1927     ok(hi != NULL, "open failed\n");
1928
1929     /* try connect without authorization */
1930     hc = InternetConnect(hi, "test.winehq.org/", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1931     ok(hc != NULL, "connect failed\n");
1932
1933     hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1934     ok(hr != NULL, "HttpOpenRequest failed\n");
1935
1936     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1937     ok(r, "HttpSendRequest failed\n");
1938
1939     sz = sizeof buffer;
1940     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1941     ok(r, "HttpQueryInfo failed\n");
1942     ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1943
1944
1945     /* set the user + password then try again */
1946     todo_wine {
1947     r = InternetSetOption(hr, INTERNET_OPTION_PROXY_USERNAME, username, 4);
1948     ok(r, "failed to set user\n");
1949
1950     r = InternetSetOption(hr, INTERNET_OPTION_PROXY_PASSWORD, password, 4);
1951     ok(r, "failed to set password\n");
1952     }
1953
1954     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1955     ok(r, "HttpSendRequest failed\n");
1956     sz = sizeof buffer;
1957     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1958     ok(r, "HttpQueryInfo failed\n");
1959     todo_wine {
1960     ok(!strcmp(buffer, "200"), "proxy code wrong\n");
1961     }
1962
1963
1964     InternetCloseHandle(hr);
1965     InternetCloseHandle(hc);
1966     InternetCloseHandle(hi);
1967 }
1968
1969 static void test_header_handling_order(int port)
1970 {
1971     static char authorization[] = "Authorization: Basic dXNlcjpwd2Q=";
1972     static char connection[]    = "Connection: Close";
1973
1974     static const char *types[2] = { "*", NULL };
1975     HINTERNET session, connect, request;
1976     DWORD size, status;
1977     BOOL ret;
1978
1979     session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1980     ok(session != NULL, "InternetOpen failed\n");
1981
1982     connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1983     ok(connect != NULL, "InternetConnect failed\n");
1984
1985     request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1986     ok(request != NULL, "HttpOpenRequest failed\n");
1987
1988     ret = HttpAddRequestHeaders(request, authorization, ~0u, HTTP_ADDREQ_FLAG_ADD);
1989     ok(ret, "HttpAddRequestHeaders failed\n");
1990
1991     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1992     ok(ret, "HttpSendRequest failed\n");
1993
1994     status = 0;
1995     size = sizeof(status);
1996     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1997     ok(ret, "HttpQueryInfo failed\n");
1998     ok(status == 200, "request failed with status %u\n", status);
1999
2000     InternetCloseHandle(request);
2001
2002     request = HttpOpenRequest(connect, NULL, "/test4", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
2003     ok(request != NULL, "HttpOpenRequest failed\n");
2004
2005     ret = HttpSendRequest(request, connection, ~0u, NULL, 0);
2006     ok(ret, "HttpSendRequest failed\n");
2007
2008     status = 0;
2009     size = sizeof(status);
2010     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2011     ok(ret, "HttpQueryInfo failed\n");
2012     ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
2013
2014     InternetCloseHandle(request);
2015
2016     request = HttpOpenRequest(connect, "POST", "/test7", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
2017     ok(request != NULL, "HttpOpenRequest failed\n");
2018
2019     ret = HttpAddRequestHeaders(request, "Content-Length: 100\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2020     ok(ret, "HttpAddRequestHeaders failed\n");
2021
2022     ret = HttpSendRequest(request, connection, ~0u, NULL, 0);
2023     ok(ret, "HttpSendRequest failed\n");
2024
2025     status = 0;
2026     size = sizeof(status);
2027     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2028     ok(ret, "HttpQueryInfo failed\n");
2029     ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
2030
2031     InternetCloseHandle(request);
2032     InternetCloseHandle(connect);
2033     InternetCloseHandle(session);
2034 }
2035
2036 static void test_connection_header(int port)
2037 {
2038     HINTERNET ses, con, req;
2039     DWORD size, status;
2040     BOOL ret;
2041
2042     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2043     ok(ses != NULL, "InternetOpen failed\n");
2044
2045     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2046     ok(con != NULL, "InternetConnect failed\n");
2047
2048     req = HttpOpenRequest(con, NULL, "/test8", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2049     ok(req != NULL, "HttpOpenRequest failed\n");
2050
2051     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2052     ok(ret, "HttpSendRequest failed\n");
2053
2054     status = 0;
2055     size = sizeof(status);
2056     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2057     ok(ret, "HttpQueryInfo failed\n");
2058     ok(status == 200, "request failed with status %u\n", status);
2059
2060     InternetCloseHandle(req);
2061
2062     req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, 0, 0);
2063     ok(req != NULL, "HttpOpenRequest failed\n");
2064
2065     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2066     ok(ret, "HttpSendRequest failed\n");
2067
2068     status = 0;
2069     size = sizeof(status);
2070     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2071     ok(ret, "HttpQueryInfo failed\n");
2072     ok(status == 200, "request failed with status %u\n", status);
2073
2074     InternetCloseHandle(req);
2075
2076     req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
2077     ok(req != NULL, "HttpOpenRequest failed\n");
2078
2079     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2080     ok(ret, "HttpSendRequest failed\n");
2081
2082     status = 0;
2083     size = sizeof(status);
2084     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2085     ok(ret, "HttpQueryInfo failed\n");
2086     ok(status == 200, "request failed with status %u\n", status);
2087
2088     InternetCloseHandle(req);
2089
2090     req = HttpOpenRequest(con, "POST", "/testA", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
2091     ok(req != NULL, "HttpOpenRequest failed\n");
2092
2093     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2094     ok(ret, "HttpSendRequest failed\n");
2095
2096     status = 0;
2097     size = sizeof(status);
2098     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2099     ok(ret, "HttpQueryInfo failed\n");
2100     ok(status == 200, "request failed with status %u\n", status);
2101
2102     InternetCloseHandle(req);
2103     InternetCloseHandle(con);
2104     InternetCloseHandle(ses);
2105 }
2106
2107 static void test_http1_1(int port)
2108 {
2109     HINTERNET ses, con, req;
2110     BOOL ret;
2111
2112     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2113     ok(ses != NULL, "InternetOpen failed\n");
2114
2115     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2116     ok(con != NULL, "InternetConnect failed\n");
2117
2118     req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2119     ok(req != NULL, "HttpOpenRequest failed\n");
2120
2121     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2122     if (ret)
2123     {
2124         InternetCloseHandle(req);
2125
2126         req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2127         ok(req != NULL, "HttpOpenRequest failed\n");
2128
2129         ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2130         ok(ret, "HttpSendRequest failed\n");
2131     }
2132
2133     InternetCloseHandle(req);
2134     InternetCloseHandle(con);
2135     InternetCloseHandle(ses);
2136 }
2137
2138 static void test_no_content(int port)
2139 {
2140     HINTERNET session, connection, req;
2141     DWORD res;
2142
2143     trace("Testing 204 no content response...\n");
2144
2145     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2146
2147     session = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
2148     ok(session != NULL,"InternetOpen failed with error %u\n", GetLastError());
2149
2150     pInternetSetStatusCallbackA(session, callback);
2151
2152     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
2153     connection = InternetConnectA(session, "localhost", port,
2154             NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
2155     ok(connection != NULL,"InternetConnect failed with error %u\n", GetLastError());
2156     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
2157
2158     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
2159     req = HttpOpenRequestA(connection, "GET", "/test_no_content", NULL, NULL, NULL,
2160             INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE, 0xdeadbead);
2161     ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
2162     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
2163
2164     SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
2165     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
2166     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
2167     SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
2168     SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
2169     SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
2170     SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
2171     SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
2172     SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
2173     SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
2174
2175     res = HttpSendRequestA(req, NULL, -1, NULL, 0);
2176     ok(!res && (GetLastError() == ERROR_IO_PENDING),
2177        "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
2178     WaitForSingleObject(hCompleteEvent, INFINITE);
2179     ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
2180
2181     CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
2182     CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
2183     CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
2184     CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
2185     CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
2186     CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
2187     CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
2188     CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
2189     CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
2190     CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
2191
2192     close_async_handle(session, hCompleteEvent, 2);
2193     CloseHandle(hCompleteEvent);
2194 }
2195
2196 static void test_conn_close(int port)
2197 {
2198     HINTERNET session, connection, req;
2199     DWORD res, avail, size;
2200     BYTE buf[1024];
2201
2202     trace("Testing connection close connection...\n");
2203
2204     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
2205     conn_close_event = CreateEvent(NULL, FALSE, FALSE, NULL);
2206
2207     session = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
2208     ok(session != NULL,"InternetOpen failed with error %u\n", GetLastError());
2209
2210     pInternetSetStatusCallbackA(session, callback);
2211
2212     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
2213     connection = InternetConnectA(session, "localhost", port,
2214             NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
2215     ok(connection != NULL,"InternetConnect failed with error %u\n", GetLastError());
2216     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
2217
2218     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
2219     req = HttpOpenRequestA(connection, "GET", "/test_conn_close", NULL, NULL, NULL,
2220             INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE, 0xdeadbead);
2221     ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
2222     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
2223
2224     SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
2225     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
2226     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
2227     SET_EXPECT(INTERNET_STATUS_SENDING_REQUEST);
2228     SET_EXPECT(INTERNET_STATUS_REQUEST_SENT);
2229     SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
2230     SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
2231     SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
2232
2233     res = HttpSendRequestA(req, NULL, -1, NULL, 0);
2234     ok(!res && (GetLastError() == ERROR_IO_PENDING),
2235        "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
2236     WaitForSingleObject(hCompleteEvent, INFINITE);
2237     ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
2238
2239     CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
2240     CHECK_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
2241     CHECK_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
2242     CHECK_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
2243     CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
2244     CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
2245     CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
2246     CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
2247
2248     avail = 0;
2249     res = InternetQueryDataAvailable(req, &avail, 0, 0);
2250     ok(res, "InternetQueryDataAvailable failed: %u\n", GetLastError());
2251     ok(avail != 0, "avail = 0\n");
2252
2253     size = 0;
2254     res = InternetReadFile(req, buf, avail, &size);
2255     ok(res, "InternetReadFile failed: %u\n", GetLastError());
2256
2257     res = InternetQueryDataAvailable(req, &avail, 0, 0);
2258     ok(!res && (GetLastError() == ERROR_IO_PENDING),
2259        "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
2260     ok(!avail, "avail = %u, expected 0\n", avail);
2261
2262     SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
2263     SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
2264     SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
2265     SetEvent(conn_close_event);
2266     WaitForSingleObject(hCompleteEvent, INFINITE);
2267     ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
2268     CHECK_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
2269     CHECK_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
2270     CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
2271
2272     close_async_handle(session, hCompleteEvent, 2);
2273     CloseHandle(hCompleteEvent);
2274 }
2275
2276 static void test_HttpSendRequestW(int port)
2277 {
2278     static const WCHAR header[] = {'U','A','-','C','P','U',':',' ','x','8','6',0};
2279     HINTERNET ses, con, req;
2280     DWORD error;
2281     BOOL ret;
2282
2283     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
2284     ok(ses != NULL, "InternetOpen failed\n");
2285
2286     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2287     ok(con != NULL, "InternetConnect failed\n");
2288
2289     req = HttpOpenRequest(con, NULL, "/test1", NULL, NULL, NULL, 0, 0);
2290     ok(req != NULL, "HttpOpenRequest failed\n");
2291
2292     SetLastError(0xdeadbeef);
2293     ret = HttpSendRequestW(req, header, ~0u, NULL, 0);
2294     error = GetLastError();
2295     ok(!ret, "HttpSendRequestW succeeded\n");
2296     ok(error == ERROR_IO_PENDING ||
2297        broken(error == ERROR_HTTP_HEADER_NOT_FOUND) ||  /* IE6 */
2298        broken(error == ERROR_INVALID_PARAMETER),        /* IE5 */
2299        "got %u expected ERROR_IO_PENDING\n", error);
2300
2301     InternetCloseHandle(req);
2302     InternetCloseHandle(con);
2303     InternetCloseHandle(ses);
2304 }
2305
2306 static void test_cookie_header(int port)
2307 {
2308     HINTERNET ses, con, req;
2309     DWORD size, status, error;
2310     BOOL ret;
2311     char buffer[64];
2312
2313     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2314     ok(ses != NULL, "InternetOpen failed\n");
2315
2316     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2317     ok(con != NULL, "InternetConnect failed\n");
2318
2319     InternetSetCookie("http://localhost", "cookie", "biscuit");
2320
2321     req = HttpOpenRequest(con, NULL, "/testC", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2322     ok(req != NULL, "HttpOpenRequest failed\n");
2323
2324     buffer[0] = 0;
2325     size = sizeof(buffer);
2326     SetLastError(0xdeadbeef);
2327     ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2328     error = GetLastError();
2329     ok(!ret, "HttpQueryInfo succeeded\n");
2330     ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "got %u expected ERROR_HTTP_HEADER_NOT_FOUND\n", error);
2331
2332     ret = HttpAddRequestHeaders(req, "Cookie: cookie=not biscuit\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD);
2333     ok(ret, "HttpAddRequestHeaders failed: %u\n", GetLastError());
2334
2335     buffer[0] = 0;
2336     size = sizeof(buffer);
2337     ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2338     ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2339     ok(!strcmp(buffer, "cookie=not biscuit"), "got '%s' expected \'cookie=not biscuit\'\n", buffer);
2340
2341     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2342     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
2343
2344     status = 0;
2345     size = sizeof(status);
2346     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2347     ok(ret, "HttpQueryInfo failed\n");
2348     ok(status == 200, "request failed with status %u\n", status);
2349
2350     buffer[0] = 0;
2351     size = sizeof(buffer);
2352     ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2353     ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2354     ok(!strcmp(buffer, "cookie=biscuit"), "got '%s' expected \'cookie=biscuit\'\n", buffer);
2355
2356     InternetCloseHandle(req);
2357     InternetCloseHandle(con);
2358     InternetCloseHandle(ses);
2359 }
2360
2361 static void test_basic_authentication(int port)
2362 {
2363     HINTERNET session, connect, request;
2364     DWORD size, status;
2365     BOOL ret;
2366
2367     session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2368     ok(session != NULL, "InternetOpen failed\n");
2369
2370     connect = InternetConnect(session, "localhost", port, "user", "pwd", INTERNET_SERVICE_HTTP, 0, 0);
2371     ok(connect != NULL, "InternetConnect failed\n");
2372
2373     request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, NULL, 0, 0);
2374     ok(request != NULL, "HttpOpenRequest failed\n");
2375
2376     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
2377     ok(ret, "HttpSendRequest failed %u\n", GetLastError());
2378
2379     status = 0;
2380     size = sizeof(status);
2381     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2382     ok(ret, "HttpQueryInfo failed\n");
2383     ok(status == 200, "request failed with status %u\n", status);
2384
2385     InternetCloseHandle(request);
2386     InternetCloseHandle(connect);
2387     InternetCloseHandle(session);
2388 }
2389
2390 static void test_invalid_response_headers(int port)
2391 {
2392     HINTERNET session, connect, request;
2393     DWORD size, status;
2394     BOOL ret;
2395     char buffer[256];
2396
2397     session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2398     ok(session != NULL, "InternetOpen failed\n");
2399
2400     connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2401     ok(connect != NULL, "InternetConnect failed\n");
2402
2403     request = HttpOpenRequest(connect, NULL, "/testE", NULL, NULL, NULL, 0, 0);
2404     ok(request != NULL, "HttpOpenRequest failed\n");
2405
2406     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
2407     ok(ret, "HttpSendRequest failed %u\n", GetLastError());
2408
2409     status = 0;
2410     size = sizeof(status);
2411     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2412     ok(ret, "HttpQueryInfo failed\n");
2413     ok(status == 401, "unexpected status %u\n", status);
2414
2415     buffer[0] = 0;
2416     size = sizeof(buffer);
2417     ret = HttpQueryInfo( request, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
2418     ok(ret, "HttpQueryInfo failed\n");
2419     ok(!strcmp(buffer, "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"),
2420        "headers wrong \"%s\"\n", buffer);
2421
2422     buffer[0] = 0;
2423     size = sizeof(buffer);
2424     ret = HttpQueryInfo( request, HTTP_QUERY_SERVER, buffer, &size, NULL);
2425     ok(ret, "HttpQueryInfo failed\n");
2426     ok(!strcmp(buffer, "winetest"), "server wrong \"%s\"\n", buffer);
2427
2428     InternetCloseHandle(request);
2429     InternetCloseHandle(connect);
2430     InternetCloseHandle(session);
2431 }
2432
2433 static void test_response_without_headers(int port)
2434 {
2435     HINTERNET hi, hc, hr;
2436     DWORD r, count, size, status;
2437     char buffer[1024];
2438
2439     SetLastError(0xdeadbeef);
2440     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2441     ok(hi != NULL, "open failed %u\n", GetLastError());
2442
2443     SetLastError(0xdeadbeef);
2444     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2445     ok(hc != NULL, "connect failed %u\n", GetLastError());
2446
2447     SetLastError(0xdeadbeef);
2448     hr = HttpOpenRequest(hc, NULL, "/testG", NULL, NULL, NULL, 0, 0);
2449     ok(hr != NULL, "HttpOpenRequest failed %u\n", GetLastError());
2450
2451     SetLastError(0xdeadbeef);
2452     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
2453     ok(r, "HttpSendRequest failed %u\n", GetLastError());
2454
2455     count = 0;
2456     memset(buffer, 0, sizeof buffer);
2457     SetLastError(0xdeadbeef);
2458     r = InternetReadFile(hr, buffer, sizeof buffer, &count);
2459     ok(r, "InternetReadFile failed %u\n", GetLastError());
2460     todo_wine ok(count == sizeof page1 - 1, "count was wrong\n");
2461     todo_wine ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
2462
2463     status = 0;
2464     size = sizeof(status);
2465     SetLastError(0xdeadbeef);
2466     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2467     todo_wine ok(r, "HttpQueryInfo failed %u\n", GetLastError());
2468     todo_wine ok(status == 200, "expected status 200 got %u\n", status);
2469
2470     buffer[0] = 0;
2471     size = sizeof(buffer);
2472     SetLastError(0xdeadbeef);
2473     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &size, NULL );
2474     ok(r, "HttpQueryInfo failed %u\n", GetLastError());
2475     ok(!strcmp(buffer, "OK"), "expected OK got: \"%s\"\n", buffer);
2476
2477     buffer[0] = 0;
2478     size = sizeof(buffer);
2479     SetLastError(0xdeadbeef);
2480     r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &size, NULL);
2481     ok(r, "HttpQueryInfo failed %u\n", GetLastError());
2482     ok(!strcmp(buffer, "HTTP/1.0"), "expected HTTP/1.0 got: \"%s\"\n", buffer);
2483
2484     buffer[0] = 0;
2485     size = sizeof(buffer);
2486     SetLastError(0xdeadbeef);
2487     r = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
2488     ok(r, "HttpQueryInfo failed %u\n", GetLastError());
2489     ok(!strcmp(buffer, "HTTP/1.0 200 OK"), "raw headers wrong: \"%s\"\n", buffer);
2490
2491     InternetCloseHandle(hr);
2492     InternetCloseHandle(hc);
2493     InternetCloseHandle(hi);
2494 }
2495
2496 static void test_HttpQueryInfo(int port)
2497 {
2498     HINTERNET hi, hc, hr;
2499     DWORD size, index;
2500     char buffer[1024];
2501     BOOL ret;
2502
2503     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2504     ok(hi != NULL, "InternetOpen failed\n");
2505
2506     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2507     ok(hc != NULL, "InternetConnect failed\n");
2508
2509     hr = HttpOpenRequest(hc, NULL, "/testD", NULL, NULL, NULL, 0, 0);
2510     ok(hr != NULL, "HttpOpenRequest failed\n");
2511
2512     ret = HttpSendRequest(hr, NULL, 0, NULL, 0);
2513     ok(ret, "HttpSendRequest failed\n");
2514
2515     index = 0;
2516     size = sizeof(buffer);
2517     ret = HttpQueryInfo(hr, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, &index);
2518     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2519     ok(index == 1, "expected 1 got %u\n", index);
2520
2521     index = 0;
2522     size = sizeof(buffer);
2523     ret = HttpQueryInfo(hr, HTTP_QUERY_DATE | HTTP_QUERY_FLAG_SYSTEMTIME, buffer, &size, &index);
2524     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2525     ok(index == 1, "expected 1 got %u\n", index);
2526
2527     index = 0;
2528     size = sizeof(buffer);
2529     ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
2530     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2531     ok(index == 0, "expected 0 got %u\n", index);
2532
2533     size = sizeof(buffer);
2534     ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
2535     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2536     ok(index == 0, "expected 0 got %u\n", index);
2537
2538     size = sizeof(buffer);
2539     ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS_CRLF, buffer, &size, &index);
2540     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2541     ok(index == 0, "expected 0 got %u\n", index);
2542
2543     size = sizeof(buffer);
2544     ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &size, &index);
2545     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2546     ok(index == 0, "expected 0 got %u\n", index);
2547
2548     size = sizeof(buffer);
2549     ret = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &size, &index);
2550     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2551     ok(index == 0, "expected 0 got %u\n", index);
2552
2553     index = 0;
2554     size = sizeof(buffer);
2555     ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &size, &index);
2556     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2557     ok(index == 0, "expected 0 got %u\n", index);
2558
2559     index = 0;
2560     size = sizeof(buffer);
2561     ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, buffer, &size, &index);
2562     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2563     ok(index == 0, "expected 0 got %u\n", index);
2564
2565     index = 0xdeadbeef;
2566     size = sizeof(buffer);
2567     ret = HttpQueryInfo(hr, HTTP_QUERY_FORWARDED, buffer, &size, &index);
2568     ok(!ret, "HttpQueryInfo succeeded\n");
2569     ok(index == 0xdeadbeef, "expected 0xdeadbeef got %u\n", index);
2570
2571     index = 0;
2572     size = sizeof(buffer);
2573     ret = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &size, &index);
2574     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2575     ok(index == 1, "expected 1 got %u\n", index);
2576
2577     index = 0;
2578     size = sizeof(buffer);
2579     strcpy(buffer, "Server");
2580     ret = HttpQueryInfo(hr, HTTP_QUERY_CUSTOM, buffer, &size, &index);
2581     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2582     ok(index == 1, "expected 1 got %u\n", index);
2583
2584     index = 0;
2585     size = sizeof(buffer);
2586     ret = HttpQueryInfo(hr, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
2587     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2588     ok(index == 1, "expected 1 got %u\n", index);
2589
2590     size = sizeof(buffer);
2591     ret = HttpQueryInfo(hr, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
2592     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2593     ok(index == 2, "expected 2 got %u\n", index);
2594
2595     InternetCloseHandle(hr);
2596     InternetCloseHandle(hc);
2597     InternetCloseHandle(hi);
2598 }
2599
2600 static void test_options(int port)
2601 {
2602     HINTERNET ses, con, req;
2603     DWORD size, error;
2604     DWORD_PTR ctx;
2605     BOOL ret;
2606
2607     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2608     ok(ses != NULL, "InternetOpen failed\n");
2609
2610     SetLastError(0xdeadbeef);
2611     ret = InternetSetOption(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, 0);
2612     error = GetLastError();
2613     ok(!ret, "InternetSetOption succeeded\n");
2614     ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
2615
2616     SetLastError(0xdeadbeef);
2617     ret = InternetSetOption(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, sizeof(ctx));
2618     ok(!ret, "InternetSetOption succeeded\n");
2619     error = GetLastError();
2620     ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
2621
2622     SetLastError(0xdeadbeef);
2623     ret = InternetSetOption(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, 0);
2624     ok(!ret, "InternetSetOption succeeded\n");
2625     error = GetLastError();
2626     ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
2627
2628     ctx = 1;
2629     ret = InternetSetOption(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
2630     ok(ret, "InternetSetOption failed %u\n", GetLastError());
2631
2632     SetLastError(0xdeadbeef);
2633     ret = InternetQueryOption(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, NULL);
2634     error = GetLastError();
2635     ok(!ret, "InternetQueryOption succeeded\n");
2636     ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
2637
2638     SetLastError(0xdeadbeef);
2639     ret = InternetQueryOption(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, NULL);
2640     error = GetLastError();
2641     ok(!ret, "InternetQueryOption succeeded\n");
2642     ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
2643
2644     size = 0;
2645     SetLastError(0xdeadbeef);
2646     ret = InternetQueryOption(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, &size);
2647     error = GetLastError();
2648     ok(!ret, "InternetQueryOption succeeded\n");
2649     ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
2650
2651     size = sizeof(ctx);
2652     SetLastError(0xdeadbeef);
2653     ret = InternetQueryOption(NULL, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
2654     error = GetLastError();
2655     ok(!ret, "InternetQueryOption succeeded\n");
2656     ok(error == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got %u\n", error);
2657
2658     ctx = 0xdeadbeef;
2659     size = sizeof(ctx);
2660     ret = InternetQueryOption(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
2661     ok(ret, "InternetQueryOption failed %u\n", GetLastError());
2662     ok(ctx == 1, "expected 1 got %lu\n", ctx);
2663
2664     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2665     ok(con != NULL, "InternetConnect failed\n");
2666
2667     ctx = 0xdeadbeef;
2668     size = sizeof(ctx);
2669     ret = InternetQueryOption(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
2670     ok(ret, "InternetQueryOption failed %u\n", GetLastError());
2671     ok(ctx == 0, "expected 0 got %lu\n", ctx);
2672
2673     ctx = 2;
2674     ret = InternetSetOption(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
2675     ok(ret, "InternetSetOption failed %u\n", GetLastError());
2676
2677     ctx = 0xdeadbeef;
2678     size = sizeof(ctx);
2679     ret = InternetQueryOption(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
2680     ok(ret, "InternetQueryOption failed %u\n", GetLastError());
2681     ok(ctx == 2, "expected 2 got %lu\n", ctx);
2682
2683     req = HttpOpenRequest(con, NULL, "/test1", NULL, NULL, NULL, 0, 0);
2684     ok(req != NULL, "HttpOpenRequest failed\n");
2685
2686     ctx = 0xdeadbeef;
2687     size = sizeof(ctx);
2688     ret = InternetQueryOption(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
2689     ok(ret, "InternetQueryOption failed %u\n", GetLastError());
2690     ok(ctx == 0, "expected 0 got %lu\n", ctx);
2691
2692     ctx = 3;
2693     ret = InternetSetOption(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
2694     ok(ret, "InternetSetOption failed %u\n", GetLastError());
2695
2696     ctx = 0xdeadbeef;
2697     size = sizeof(ctx);
2698     ret = InternetQueryOption(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
2699     ok(ret, "InternetQueryOption failed %u\n", GetLastError());
2700     ok(ctx == 3, "expected 3 got %lu\n", ctx);
2701
2702     /* INTERNET_OPTION_PROXY */
2703     SetLastError(0xdeadbeef);
2704     ret = InternetQueryOptionA(ses, INTERNET_OPTION_PROXY, NULL, NULL);
2705     error = GetLastError();
2706     ok(!ret, "InternetQueryOption succeeded\n");
2707     ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
2708
2709     SetLastError(0xdeadbeef);
2710     ret = InternetQueryOptionA(ses, INTERNET_OPTION_PROXY, &ctx, NULL);
2711     error = GetLastError();
2712     ok(!ret, "InternetQueryOption succeeded\n");
2713     ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
2714
2715     size = 0;
2716     SetLastError(0xdeadbeef);
2717     ret = InternetQueryOptionA(ses, INTERNET_OPTION_PROXY, NULL, &size);
2718     error = GetLastError();
2719     ok(!ret, "InternetQueryOption succeeded\n");
2720     ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
2721     ok(size >= sizeof(INTERNET_PROXY_INFOA), "expected size to be greater or equal to the struct size\n");
2722
2723     InternetCloseHandle(req);
2724     InternetCloseHandle(con);
2725     InternetCloseHandle(ses);
2726 }
2727
2728 static void test_url_caching(int port, int *num_retrievals)
2729 {
2730     HINTERNET hi, hc, hr;
2731     DWORD r, count;
2732     char buffer[0x100];
2733
2734     ok(*num_retrievals == 0, "expected 0 retrievals prior to test\n");
2735
2736     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2737     ok(hi != NULL, "open failed\n");
2738
2739     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2740     ok(hc != NULL, "connect failed\n");
2741
2742     /* Pre-load the cache: */
2743     hr = HttpOpenRequest(hc, "GET", "/testH", NULL, NULL, NULL, 0, 0);
2744     ok(hr != NULL, "HttpOpenRequest failed\n");
2745
2746     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
2747     ok(r, "HttpSendRequest failed\n");
2748
2749     ok(*num_retrievals == 1, "expected 1 retrievals, got %d\n", *num_retrievals);
2750
2751     count = 0;
2752     memset(buffer, 0, sizeof buffer);
2753     SetLastError(0xdeadbeef);
2754     r = InternetReadFile(hr, buffer, sizeof buffer, &count);
2755     ok(r, "InternetReadFile failed %u\n", GetLastError());
2756     ok(count == sizeof page1 - 1, "count was wrong\n");
2757     ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
2758
2759     InternetCloseHandle(hr);
2760
2761     /* Send the same request, requiring it to be retrieved from the cache */
2762     hr = HttpOpenRequest(hc, "GET", "/testH", NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
2763     ok(hr != NULL, "HttpOpenRequest failed\n");
2764
2765     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
2766     /* Older Windows versions succeed with this request, newer ones fail with
2767      * ERROR_FILE_NOT_FOUND.  Accept either, as the older version allows us
2768      * to verify that the server isn't contacted.
2769      */
2770     if (!r)
2771         ok(GetLastError() == ERROR_FILE_NOT_FOUND,
2772            "expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
2773     else
2774     {
2775         /* The server shouldn't be contacted for this request. */
2776         todo_wine
2777         ok(*num_retrievals == 1, "expected 1 retrievals\n");
2778
2779         count = 0;
2780         memset(buffer, 0, sizeof buffer);
2781         SetLastError(0xdeadbeef);
2782         r = InternetReadFile(hr, buffer, sizeof buffer, &count);
2783         ok(r, "InternetReadFile failed %u\n", GetLastError());
2784         ok(count == sizeof page1 - 1, "count was wrong\n");
2785         ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
2786     }
2787
2788     InternetCloseHandle(hc);
2789     InternetCloseHandle(hi);
2790 }
2791
2792 static void test_http_connection(void)
2793 {
2794     struct server_info si;
2795     HANDLE hThread;
2796     DWORD id = 0, r;
2797
2798     si.hEvent = CreateEvent(NULL, 0, 0, NULL);
2799     si.port = 7531;
2800     si.num_testH_retrievals = 0;
2801
2802     hThread = CreateThread(NULL, 0, server_thread, (LPVOID) &si, 0, &id);
2803     ok( hThread != NULL, "create thread failed\n");
2804
2805     r = WaitForSingleObject(si.hEvent, 10000);
2806     ok (r == WAIT_OBJECT_0, "failed to start wininet test server\n");
2807     if (r != WAIT_OBJECT_0)
2808         return;
2809
2810     test_basic_request(si.port, "GET", "/test1");
2811     test_proxy_indirect(si.port);
2812     test_proxy_direct(si.port);
2813     test_header_handling_order(si.port);
2814     test_basic_request(si.port, "POST", "/test5");
2815     test_basic_request(si.port, "RPC_IN_DATA", "/test5");
2816     test_basic_request(si.port, "RPC_OUT_DATA", "/test5");
2817     test_basic_request(si.port, "GET", "/test6");
2818     test_basic_request(si.port, "GET", "/testF");
2819     test_connection_header(si.port);
2820     test_http1_1(si.port);
2821     test_cookie_header(si.port);
2822     test_basic_authentication(si.port);
2823     test_invalid_response_headers(si.port);
2824     test_response_without_headers(si.port);
2825     test_HttpQueryInfo(si.port);
2826     test_HttpSendRequestW(si.port);
2827     test_last_error(si.port);
2828     test_options(si.port);
2829     test_url_caching(si.port, &si.num_testH_retrievals);
2830     test_no_content(si.port);
2831     test_conn_close(si.port);
2832
2833     /* send the basic request again to shutdown the server thread */
2834     test_basic_request(si.port, "GET", "/quit");
2835
2836     r = WaitForSingleObject(hThread, 3000);
2837     ok( r == WAIT_OBJECT_0, "thread wait failed\n");
2838     CloseHandle(hThread);
2839 }
2840
2841 static void release_cert_info(INTERNET_CERTIFICATE_INFOA *info)
2842 {
2843     LocalFree(info->lpszSubjectInfo);
2844     LocalFree(info->lpszIssuerInfo);
2845     LocalFree(info->lpszProtocolName);
2846     LocalFree(info->lpszSignatureAlgName);
2847     LocalFree(info->lpszEncryptionAlgName);
2848 }
2849
2850 static void test_secure_connection(void)
2851 {
2852     static const WCHAR gizmo5[] = {'G','i','z','m','o','5',0};
2853     static const WCHAR testbot[] = {'t','e','s','t','b','o','t','.','w','i','n','e','h','q','.','o','r','g',0};
2854     static const WCHAR get[] = {'G','E','T',0};
2855     static const WCHAR slash[] = {'/',0};
2856     HINTERNET ses, con, req;
2857     DWORD size, flags;
2858     INTERNET_CERTIFICATE_INFOA *certificate_structA = NULL;
2859     INTERNET_CERTIFICATE_INFOW *certificate_structW = NULL;
2860     BOOL ret;
2861
2862     ses = InternetOpen("Gizmo5", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
2863     ok(ses != NULL, "InternetOpen failed\n");
2864
2865     con = InternetConnect(ses, "testbot.winehq.org",
2866                           INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL,
2867                           INTERNET_SERVICE_HTTP, 0, 0);
2868     ok(con != NULL, "InternetConnect failed\n");
2869
2870     req = HttpOpenRequest(con, "GET", "/", NULL, NULL, NULL,
2871                           INTERNET_FLAG_SECURE, 0);
2872     ok(req != NULL, "HttpOpenRequest failed\n");
2873
2874     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2875     ok(ret, "HttpSendRequest failed: %d\n", GetLastError());
2876
2877     size = sizeof(flags);
2878     ret = InternetQueryOption(req, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
2879     ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
2880     ok(flags & SECURITY_FLAG_SECURE, "expected secure flag to be set\n");
2881
2882     ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2883                                NULL, &size);
2884     ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
2885     ok(size == sizeof(INTERNET_CERTIFICATE_INFOA), "size = %d\n", size);
2886     certificate_structA = HeapAlloc(GetProcessHeap(), 0, size);
2887     ret = InternetQueryOption(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2888                               certificate_structA, &size);
2889     ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
2890     if (ret)
2891     {
2892         ok(certificate_structA->lpszSubjectInfo &&
2893            strlen(certificate_structA->lpszSubjectInfo) > 1,
2894            "expected a non-empty subject name\n");
2895         ok(certificate_structA->lpszIssuerInfo &&
2896            strlen(certificate_structA->lpszIssuerInfo) > 1,
2897            "expected a non-empty issuer name\n");
2898         ok(!certificate_structA->lpszSignatureAlgName,
2899            "unexpected signature algorithm name\n");
2900         ok(!certificate_structA->lpszEncryptionAlgName,
2901            "unexpected encryption algorithm name\n");
2902         ok(!certificate_structA->lpszProtocolName,
2903            "unexpected protocol name\n");
2904         ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
2905         release_cert_info(certificate_structA);
2906     }
2907     HeapFree(GetProcessHeap(), 0, certificate_structA);
2908
2909     /* Querying the same option through InternetQueryOptionW still results in
2910      * ASCII strings being returned.
2911      */
2912     size = 0;
2913     ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2914                                NULL, &size);
2915     ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
2916     ok(size == sizeof(INTERNET_CERTIFICATE_INFOW), "size = %d\n", size);
2917     certificate_structW = HeapAlloc(GetProcessHeap(), 0, size);
2918     ret = InternetQueryOption(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2919                               certificate_structW, &size);
2920     certificate_structA = (INTERNET_CERTIFICATE_INFOA *)certificate_structW;
2921     ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
2922     if (ret)
2923     {
2924         ok(certificate_structA->lpszSubjectInfo &&
2925            strlen(certificate_structA->lpszSubjectInfo) > 1,
2926            "expected a non-empty subject name\n");
2927         ok(certificate_structA->lpszIssuerInfo &&
2928            strlen(certificate_structA->lpszIssuerInfo) > 1,
2929            "expected a non-empty issuer name\n");
2930         ok(!certificate_structA->lpszSignatureAlgName,
2931            "unexpected signature algorithm name\n");
2932         ok(!certificate_structA->lpszEncryptionAlgName,
2933            "unexpected encryption algorithm name\n");
2934         ok(!certificate_structA->lpszProtocolName,
2935            "unexpected protocol name\n");
2936         ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
2937         release_cert_info(certificate_structA);
2938     }
2939     HeapFree(GetProcessHeap(), 0, certificate_structW);
2940
2941     InternetCloseHandle(req);
2942     InternetCloseHandle(con);
2943     InternetCloseHandle(ses);
2944
2945     /* Repeating the tests with the W functions has the same result: */
2946     ses = InternetOpenW(gizmo5, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
2947     ok(ses != NULL, "InternetOpen failed\n");
2948
2949     con = InternetConnectW(ses, testbot,
2950                           INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL,
2951                           INTERNET_SERVICE_HTTP, 0, 0);
2952     ok(con != NULL, "InternetConnect failed\n");
2953
2954     req = HttpOpenRequestW(con, get, slash, NULL, NULL, NULL,
2955                           INTERNET_FLAG_SECURE, 0);
2956     ok(req != NULL, "HttpOpenRequest failed\n");
2957
2958     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2959     ok(ret, "HttpSendRequest failed: %d\n", GetLastError());
2960
2961     size = sizeof(flags);
2962     ret = InternetQueryOption(req, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
2963     ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
2964     ok(flags & SECURITY_FLAG_SECURE, "expected secure flag to be set\n");
2965
2966     ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2967                                NULL, &size);
2968     ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
2969     ok(size == sizeof(INTERNET_CERTIFICATE_INFOA), "size = %d\n", size);
2970     certificate_structA = HeapAlloc(GetProcessHeap(), 0, size);
2971     ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2972                                certificate_structA, &size);
2973     ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
2974     if (ret)
2975     {
2976         ok(certificate_structA->lpszSubjectInfo &&
2977            strlen(certificate_structA->lpszSubjectInfo) > 1,
2978            "expected a non-empty subject name\n");
2979         ok(certificate_structA->lpszIssuerInfo &&
2980            strlen(certificate_structA->lpszIssuerInfo) > 1,
2981            "expected a non-empty issuer name\n");
2982         ok(!certificate_structA->lpszSignatureAlgName,
2983            "unexpected signature algorithm name\n");
2984         ok(!certificate_structA->lpszEncryptionAlgName,
2985            "unexpected encryption algorithm name\n");
2986         ok(!certificate_structA->lpszProtocolName,
2987            "unexpected protocol name\n");
2988         ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
2989         release_cert_info(certificate_structA);
2990     }
2991     HeapFree(GetProcessHeap(), 0, certificate_structA);
2992
2993     /* Again, querying the same option through InternetQueryOptionW still
2994      * results in ASCII strings being returned.
2995      */
2996     size = 0;
2997     ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2998                                NULL, &size);
2999     ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
3000     ok(size == sizeof(INTERNET_CERTIFICATE_INFOW), "size = %d\n", size);
3001     certificate_structW = HeapAlloc(GetProcessHeap(), 0, size);
3002     ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
3003                                certificate_structW, &size);
3004     certificate_structA = (INTERNET_CERTIFICATE_INFOA *)certificate_structW;
3005     ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
3006     if (ret)
3007     {
3008         ok(certificate_structA->lpszSubjectInfo &&
3009            strlen(certificate_structA->lpszSubjectInfo) > 1,
3010            "expected a non-empty subject name\n");
3011         ok(certificate_structA->lpszIssuerInfo &&
3012            strlen(certificate_structA->lpszIssuerInfo) > 1,
3013            "expected a non-empty issuer name\n");
3014         ok(!certificate_structA->lpszSignatureAlgName,
3015            "unexpected signature algorithm name\n");
3016         ok(!certificate_structA->lpszEncryptionAlgName,
3017            "unexpected encryption algorithm name\n");
3018         ok(!certificate_structA->lpszProtocolName,
3019            "unexpected protocol name\n");
3020         ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
3021         release_cert_info(certificate_structA);
3022     }
3023     HeapFree(GetProcessHeap(), 0, certificate_structW);
3024
3025     InternetCloseHandle(req);
3026     InternetCloseHandle(con);
3027     InternetCloseHandle(ses);
3028 }
3029
3030 static void test_user_agent_header(void)
3031 {
3032     HINTERNET ses, con, req;
3033     DWORD size, err;
3034     char buffer[64];
3035     BOOL ret;
3036
3037     ses = InternetOpen("Gizmo5", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
3038     ok(ses != NULL, "InternetOpen failed\n");
3039
3040     con = InternetConnect(ses, "test.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3041     ok(con != NULL, "InternetConnect failed\n");
3042
3043     req = HttpOpenRequest(con, "GET", "/tests/hello.html", "HTTP/1.0", NULL, NULL, 0, 0);
3044     ok(req != NULL, "HttpOpenRequest failed\n");
3045
3046     size = sizeof(buffer);
3047     ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
3048     err = GetLastError();
3049     ok(!ret, "HttpQueryInfo succeeded\n");
3050     ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
3051
3052     ret = HttpAddRequestHeaders(req, "User-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
3053     ok(ret, "HttpAddRequestHeaders succeeded\n");
3054
3055     size = sizeof(buffer);
3056     ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
3057     err = GetLastError();
3058     ok(ret, "HttpQueryInfo failed\n");
3059     ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
3060
3061     InternetCloseHandle(req);
3062
3063     req = HttpOpenRequest(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
3064     ok(req != NULL, "HttpOpenRequest failed\n");
3065
3066     size = sizeof(buffer);
3067     ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
3068     err = GetLastError();
3069     ok(!ret, "HttpQueryInfo succeeded\n");
3070     ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
3071
3072     ret = HttpAddRequestHeaders(req, "Accept: audio/*, image/*, text/*\r\nUser-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
3073     ok(ret, "HttpAddRequestHeaders failed\n");
3074
3075     buffer[0] = 0;
3076     size = sizeof(buffer);
3077     ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
3078     ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
3079     ok(!strcmp(buffer, "audio/*, image/*, text/*"), "got '%s' expected 'audio/*, image/*, text/*'\n", buffer);
3080
3081     InternetCloseHandle(req);
3082     InternetCloseHandle(con);
3083     InternetCloseHandle(ses);
3084 }
3085
3086 static void test_bogus_accept_types_array(void)
3087 {
3088     HINTERNET ses, con, req;
3089     static const char *types[] = { (const char *)6240, "*/*", "%p", "", (const char *)0xffffffff, "*/*", NULL };
3090     DWORD size, error;
3091     char buffer[32];
3092     BOOL ret;
3093
3094     ses = InternetOpen("MERONG(0.9/;p)", INTERNET_OPEN_TYPE_DIRECT, "", "", 0);
3095     con = InternetConnect(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3096     req = HttpOpenRequest(con, "POST", "/post/post_action.php", "HTTP/1.0", "", types, INTERNET_FLAG_FORMS_SUBMIT, 0);
3097
3098     ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
3099
3100     buffer[0] = 0;
3101     size = sizeof(buffer);
3102     SetLastError(0xdeadbeef);
3103     ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
3104     error = GetLastError();
3105     ok(!ret || broken(ret), "HttpQueryInfo succeeded\n");
3106     if (!ret) ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", error);
3107     ok(broken(!strcmp(buffer, ", */*, %p, , , */*")) /* IE6 */ ||
3108        broken(!strcmp(buffer, "*/*, %p, */*")) /* IE7/8 */ ||
3109        !strcmp(buffer, ""), "got '%s' expected ''\n", buffer);
3110
3111     InternetCloseHandle(req);
3112     InternetCloseHandle(con);
3113     InternetCloseHandle(ses);
3114 }
3115
3116 struct context
3117 {
3118     HANDLE event;
3119     HINTERNET req;
3120 };
3121
3122 static void WINAPI cb(HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID info, DWORD size)
3123 {
3124     INTERNET_ASYNC_RESULT *result = info;
3125     struct context *ctx = (struct context *)context;
3126
3127     trace("%p 0x%08lx %u %p 0x%08x\n", handle, context, status, info, size);
3128
3129     if (status == INTERNET_STATUS_REQUEST_COMPLETE)
3130     {
3131         trace("request handle: 0x%08lx\n", result->dwResult);
3132         ctx->req = (HINTERNET)result->dwResult;
3133         SetEvent(ctx->event);
3134     }
3135     if (status == INTERNET_STATUS_HANDLE_CLOSING)
3136     {
3137         DWORD type = INTERNET_HANDLE_TYPE_CONNECT_HTTP, size = sizeof(type);
3138
3139         if (InternetQueryOption(handle, INTERNET_OPTION_HANDLE_TYPE, &type, &size))
3140             ok(type != INTERNET_HANDLE_TYPE_CONNECT_HTTP, "unexpected callback\n");
3141         SetEvent(ctx->event);
3142     }
3143 }
3144
3145 static void test_open_url_async(void)
3146 {
3147     BOOL ret;
3148     HINTERNET ses, req;
3149     DWORD size, error;
3150     struct context ctx;
3151     ULONG type;
3152
3153     ctx.req = NULL;
3154     ctx.event = CreateEvent(NULL, TRUE, FALSE, "Z:_home_hans_jaman-installer.exe_ev1");
3155
3156     ses = InternetOpen("AdvancedInstaller", 0, NULL, NULL, INTERNET_FLAG_ASYNC);
3157     ok(ses != NULL, "InternetOpen failed\n");
3158
3159     SetLastError(0xdeadbeef);
3160     ret = InternetSetOptionA(NULL, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
3161     error = GetLastError();
3162     ok(!ret, "InternetSetOptionA succeeded\n");
3163     ok(error == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "got %u expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE\n", error);
3164
3165     ret = InternetSetOptionA(ses, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
3166     error = GetLastError();
3167     ok(!ret, "InternetSetOptionA failed\n");
3168     ok(error == ERROR_INTERNET_OPTION_NOT_SETTABLE, "got %u expected ERROR_INTERNET_OPTION_NOT_SETTABLE\n", error);
3169
3170     pInternetSetStatusCallbackA(ses, cb);
3171     ResetEvent(ctx.event);
3172
3173     req = InternetOpenUrl(ses, "http://test.winehq.org", NULL, 0, 0, (DWORD_PTR)&ctx);
3174     ok(!req && GetLastError() == ERROR_IO_PENDING, "InternetOpenUrl failed\n");
3175
3176     WaitForSingleObject(ctx.event, INFINITE);
3177
3178     type = 0;
3179     size = sizeof(type);
3180     ret = InternetQueryOption(ctx.req, INTERNET_OPTION_HANDLE_TYPE, &type, &size);
3181     ok(ret, "InternetQueryOption failed: %u\n", GetLastError());
3182     ok(type == INTERNET_HANDLE_TYPE_HTTP_REQUEST,
3183        "expected INTERNET_HANDLE_TYPE_HTTP_REQUEST, got %u\n", type);
3184
3185     size = 0;
3186     ret = HttpQueryInfo(ctx.req, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &size, NULL);
3187     ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "HttpQueryInfo failed\n");
3188     ok(size > 0, "expected size > 0\n");
3189
3190     ResetEvent(ctx.event);
3191     InternetCloseHandle(ctx.req);
3192     WaitForSingleObject(ctx.event, INFINITE);
3193
3194     InternetCloseHandle(ses);
3195     CloseHandle(ctx.event);
3196 }
3197
3198 enum api
3199 {
3200     internet_connect = 1,
3201     http_open_request,
3202     http_send_request_ex,
3203     internet_writefile,
3204     http_end_request,
3205     internet_close_handle
3206 };
3207
3208 struct notification
3209 {
3210     enum api     function; /* api responsible for notification */
3211     unsigned int status;   /* status received */
3212     int          async;    /* delivered from another thread? */
3213     int          todo;
3214     int          optional;
3215 };
3216
3217 struct info
3218 {
3219     enum api     function;
3220     const struct notification *test;
3221     unsigned int count;
3222     unsigned int index;
3223     HANDLE       wait;
3224     DWORD        thread;
3225     unsigned int line;
3226     DWORD        expect_result;
3227     BOOL         is_aborted;
3228 };
3229
3230 static CRITICAL_SECTION notification_cs;
3231
3232 static void CALLBACK check_notification( HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID buffer, DWORD buflen )
3233 {
3234     BOOL status_ok, function_ok;
3235     struct info *info = (struct info *)context;
3236     unsigned int i;
3237
3238     EnterCriticalSection( &notification_cs );
3239
3240     if(info->is_aborted) {
3241         LeaveCriticalSection(&notification_cs);
3242         return;
3243     }
3244
3245     if (status == INTERNET_STATUS_HANDLE_CREATED)
3246     {
3247         DWORD size = sizeof(struct info *);
3248         HttpQueryInfoA( handle, INTERNET_OPTION_CONTEXT_VALUE, &info, &size, 0 );
3249     }else if(status == INTERNET_STATUS_REQUEST_COMPLETE) {
3250         INTERNET_ASYNC_RESULT *ar = (INTERNET_ASYNC_RESULT*)buffer;
3251
3252         ok(buflen == sizeof(*ar), "unexpected buflen = %d\n", buflen);
3253         if(info->expect_result == ERROR_SUCCESS) {
3254             ok(ar->dwResult == 1, "ar->dwResult = %ld, expected 1\n", ar->dwResult);
3255         }else {
3256             ok(!ar->dwResult, "ar->dwResult = %ld, expected 1\n", ar->dwResult);
3257             ok(ar->dwError == info->expect_result, "ar->dwError = %d, expected %d\n", ar->dwError, info->expect_result);
3258         }
3259     }
3260
3261     i = info->index;
3262     if (i >= info->count)
3263     {
3264         LeaveCriticalSection( &notification_cs );
3265         return;
3266     }
3267
3268     while (info->test[i].status != status &&
3269         (info->test[i].optional || info->test[i].todo) &&
3270         i < info->count - 1 &&
3271         info->test[i].function == info->test[i + 1].function)
3272     {
3273         i++;
3274     }
3275
3276     status_ok   = (info->test[i].status == status);
3277     function_ok = (info->test[i].function == info->function);
3278
3279     if (!info->test[i].todo)
3280     {
3281         ok( status_ok, "%u: expected status %u got %u\n", info->line, info->test[i].status, status );
3282         ok( function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function );
3283
3284         if (info->test[i].async)
3285             ok(info->thread != GetCurrentThreadId(), "%u: expected thread %u got %u\n",
3286                info->line, info->thread, GetCurrentThreadId());
3287     }
3288     else
3289     {
3290         todo_wine ok( status_ok, "%u: expected status %u got %u\n", info->line, info->test[i].status, status );
3291         if (status_ok)
3292             todo_wine ok( function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function );
3293     }
3294     if (i == info->count - 1 || info->test[i].function != info->test[i + 1].function) SetEvent( info->wait );
3295     info->index = i+1;
3296
3297     LeaveCriticalSection( &notification_cs );
3298 }
3299
3300 static void setup_test( struct info *info, enum api function, unsigned int line, DWORD expect_result )
3301 {
3302     info->function = function;
3303     info->line = line;
3304     info->expect_result = expect_result;
3305 }
3306
3307 struct notification_data
3308 {
3309     const struct notification *test;
3310     const unsigned int count;
3311     const char *method;
3312     const char *host;
3313     const char *path;
3314     const char *data;
3315     BOOL expect_conn_failure;
3316 };
3317
3318 static const struct notification async_send_request_ex_test[] =
3319 {
3320     { internet_connect,      INTERNET_STATUS_HANDLE_CREATED, 0 },
3321     { http_open_request,     INTERNET_STATUS_HANDLE_CREATED, 0 },
3322     { http_send_request_ex,  INTERNET_STATUS_DETECTING_PROXY, 1, 0, 1 },
3323     { http_send_request_ex,  INTERNET_STATUS_COOKIE_SENT, 1, 0, 1 },
3324     { http_send_request_ex,  INTERNET_STATUS_RESOLVING_NAME, 1, 0, 1 },
3325     { http_send_request_ex,  INTERNET_STATUS_NAME_RESOLVED, 1, 0, 1 },
3326     { http_send_request_ex,  INTERNET_STATUS_CONNECTING_TO_SERVER, 1 },
3327     { http_send_request_ex,  INTERNET_STATUS_CONNECTED_TO_SERVER, 1 },
3328     { http_send_request_ex,  INTERNET_STATUS_SENDING_REQUEST, 1 },
3329     { http_send_request_ex,  INTERNET_STATUS_REQUEST_SENT, 1 },
3330     { http_send_request_ex,  INTERNET_STATUS_REQUEST_COMPLETE, 1 },
3331     { internet_writefile,    INTERNET_STATUS_SENDING_REQUEST, 0 },
3332     { internet_writefile,    INTERNET_STATUS_REQUEST_SENT, 0 },
3333     { http_end_request,      INTERNET_STATUS_RECEIVING_RESPONSE, 1 },
3334     { http_end_request,      INTERNET_STATUS_RESPONSE_RECEIVED, 1 },
3335     { http_end_request,      INTERNET_STATUS_REQUEST_COMPLETE, 1 },
3336     { internet_close_handle, INTERNET_STATUS_CLOSING_CONNECTION, 0, 0, 1 },
3337     { internet_close_handle, INTERNET_STATUS_CONNECTION_CLOSED, 0, 0, 1 },
3338     { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, },
3339     { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, }
3340 };
3341
3342 static const struct notification async_send_request_ex_test2[] =
3343 {
3344     { internet_connect,      INTERNET_STATUS_HANDLE_CREATED, 0 },
3345     { http_open_request,     INTERNET_STATUS_HANDLE_CREATED, 0 },
3346     { http_send_request_ex,  INTERNET_STATUS_DETECTING_PROXY, 1, 0, 1 },
3347     { http_send_request_ex,  INTERNET_STATUS_COOKIE_SENT, 1, 0, 1 },
3348     { http_send_request_ex,  INTERNET_STATUS_RESOLVING_NAME, 1, 0, 1 },
3349     { http_send_request_ex,  INTERNET_STATUS_NAME_RESOLVED, 1, 0, 1 },
3350     { http_send_request_ex,  INTERNET_STATUS_CONNECTING_TO_SERVER, 1, 1 },
3351     { http_send_request_ex,  INTERNET_STATUS_CONNECTED_TO_SERVER, 1, 1 },
3352     { http_send_request_ex,  INTERNET_STATUS_SENDING_REQUEST, 1 },
3353     { http_send_request_ex,  INTERNET_STATUS_REQUEST_SENT, 1 },
3354     { http_send_request_ex,  INTERNET_STATUS_REQUEST_COMPLETE, 1 },
3355     { http_end_request,      INTERNET_STATUS_RECEIVING_RESPONSE, 1 },
3356     { http_end_request,      INTERNET_STATUS_RESPONSE_RECEIVED, 1 },
3357     { http_end_request,      INTERNET_STATUS_REQUEST_COMPLETE, 1 },
3358     { internet_close_handle, INTERNET_STATUS_CLOSING_CONNECTION, 0, 0, 1 },
3359     { internet_close_handle, INTERNET_STATUS_CONNECTION_CLOSED, 0, 0, 1 },
3360     { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, },
3361     { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, }
3362 };
3363
3364 static const struct notification async_send_request_ex_resolve_failure_test[] =
3365 {
3366     { internet_connect,      INTERNET_STATUS_HANDLE_CREATED, 0 },
3367     { http_open_request,     INTERNET_STATUS_HANDLE_CREATED, 0 },
3368     { http_send_request_ex,  INTERNET_STATUS_DETECTING_PROXY, 1, 0, 1 },
3369     { http_send_request_ex,  INTERNET_STATUS_RESOLVING_NAME, 1 },
3370     { http_send_request_ex,  INTERNET_STATUS_DETECTING_PROXY, 1, 0, 1 },
3371     { http_send_request_ex,  INTERNET_STATUS_REQUEST_COMPLETE, 1 },
3372     { http_end_request,      INTERNET_STATUS_REQUEST_COMPLETE, 1 },
3373     { internet_close_handle, INTERNET_STATUS_CLOSING_CONNECTION, 0, 0, 1 },
3374     { internet_close_handle, INTERNET_STATUS_CONNECTION_CLOSED, 0, 0, 1 },
3375     { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, },
3376     { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, }
3377 };
3378
3379 static const struct notification_data notification_data[] = {
3380     {
3381         async_send_request_ex_test,
3382         sizeof(async_send_request_ex_test)/sizeof(async_send_request_ex_test[0]),
3383         "POST",
3384         "test.winehq.org",
3385         "tests/posttest.php",
3386         "Public ID=codeweavers"
3387     },
3388     {
3389         async_send_request_ex_test2,
3390         sizeof(async_send_request_ex_test)/sizeof(async_send_request_ex_test[0]),
3391         "POST",
3392         "test.winehq.org",
3393         "tests/posttest.php"
3394     },
3395     {
3396         async_send_request_ex_resolve_failure_test,
3397         sizeof(async_send_request_ex_resolve_failure_test)/sizeof(async_send_request_ex_resolve_failure_test[0]),
3398         "GET",
3399         "brokenhost",
3400         "index.html",
3401         NULL,
3402         TRUE
3403     }
3404 };
3405
3406 static void test_async_HttpSendRequestEx(const struct notification_data *nd)
3407 {
3408     BOOL ret;
3409     HINTERNET ses, req, con;
3410     struct info info;
3411     DWORD size, written, error;
3412     INTERNET_BUFFERSA b;
3413     static const char *accept[2] = {"*/*", NULL};
3414     char buffer[32];
3415
3416     trace("Async HttpSendRequestEx test (%s %s)\n", nd->method, nd->host);
3417
3418     InitializeCriticalSection( &notification_cs );
3419
3420     info.test  = nd->test;
3421     info.count = nd->count;
3422     info.index = 0;
3423     info.wait = CreateEvent( NULL, FALSE, FALSE, NULL );
3424     info.thread = GetCurrentThreadId();
3425     info.is_aborted = FALSE;
3426
3427     ses = InternetOpen( "winetest", 0, NULL, NULL, INTERNET_FLAG_ASYNC );
3428     ok( ses != NULL, "InternetOpen failed\n" );
3429
3430     pInternetSetStatusCallbackA( ses, check_notification );
3431
3432     setup_test( &info, internet_connect, __LINE__, ERROR_SUCCESS );
3433     con = InternetConnect( ses, nd->host, 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, (DWORD_PTR)&info );
3434     ok( con != NULL, "InternetConnect failed %u\n", GetLastError() );
3435
3436     WaitForSingleObject( info.wait, 10000 );
3437
3438     setup_test( &info, http_open_request, __LINE__, ERROR_SUCCESS );
3439     req = HttpOpenRequest( con, nd->method, nd->path, NULL, NULL, accept, 0, (DWORD_PTR)&info );
3440     ok( req != NULL, "HttpOpenRequest failed %u\n", GetLastError() );
3441
3442     WaitForSingleObject( info.wait, 10000 );
3443
3444     if(nd->data) {
3445         memset( &b, 0, sizeof(INTERNET_BUFFERSA) );
3446         b.dwStructSize = sizeof(INTERNET_BUFFERSA);
3447         b.lpcszHeader = "Content-Type: application/x-www-form-urlencoded";
3448         b.dwHeadersLength = strlen( b.lpcszHeader );
3449         b.dwBufferTotal = nd->data ? strlen( nd->data ) : 0;
3450     }
3451
3452     setup_test( &info, http_send_request_ex, __LINE__,
3453             nd->expect_conn_failure ? ERROR_INTERNET_NAME_NOT_RESOLVED : ERROR_SUCCESS );
3454     ret = HttpSendRequestExA( req, nd->data ? &b : NULL, NULL, 0x28, 0 );
3455     ok( !ret && GetLastError() == ERROR_IO_PENDING, "HttpSendRequestExA failed %d %u\n", ret, GetLastError() );
3456
3457     error = WaitForSingleObject( info.wait, 10000 );
3458     if(error != WAIT_OBJECT_0) {
3459         skip("WaitForSingleObject returned %d, assuming DNS problem\n", error);
3460         info.is_aborted = TRUE;
3461         goto abort;
3462     }
3463
3464     size = sizeof(buffer);
3465     SetLastError( 0xdeadbeef );
3466     ret = HttpQueryInfoA( req, HTTP_QUERY_CONTENT_ENCODING, buffer, &size, 0 );
3467     error = GetLastError();
3468     ok( !ret, "HttpQueryInfoA failed %u\n", GetLastError() );
3469     if(nd->expect_conn_failure) {
3470         ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND got %u\n", error );
3471     }else {
3472         todo_wine
3473         ok(error == ERROR_INTERNET_INCORRECT_HANDLE_STATE,
3474             "expected ERROR_INTERNET_INCORRECT_HANDLE_STATE got %u\n", error );
3475     }
3476
3477     if (nd->data)
3478     {
3479         written = 0;
3480         size = strlen( nd->data );
3481         setup_test( &info, internet_writefile, __LINE__, ERROR_SUCCESS );
3482         ret = InternetWriteFile( req, nd->data, size, &written );
3483         ok( ret, "InternetWriteFile failed %u\n", GetLastError() );
3484         ok( written == size, "expected %u got %u\n", written, size );
3485
3486         WaitForSingleObject( info.wait, 10000 );
3487
3488         SetLastError( 0xdeadbeef );
3489         ret = HttpEndRequestA( req, (void *)nd->data, 0x28, 0 );
3490         error = GetLastError();
3491         ok( !ret, "HttpEndRequestA succeeded\n" );
3492         ok( error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER got %u\n", error );
3493     }
3494
3495     SetLastError( 0xdeadbeef );
3496     setup_test( &info, http_end_request, __LINE__,
3497             nd->expect_conn_failure ? ERROR_INTERNET_OPERATION_CANCELLED : ERROR_SUCCESS);
3498     ret = HttpEndRequestA( req, NULL, 0x28, 0 );
3499     error = GetLastError();
3500     ok( !ret, "HttpEndRequestA succeeded\n" );
3501     ok( error == ERROR_IO_PENDING, "expected ERROR_IO_PENDING got %u\n", error );
3502
3503     WaitForSingleObject( info.wait, 10000 );
3504
3505     setup_test( &info, internet_close_handle, __LINE__, ERROR_SUCCESS );
3506  abort:
3507     InternetCloseHandle( req );
3508     InternetCloseHandle( con );
3509     InternetCloseHandle( ses );
3510
3511     WaitForSingleObject( info.wait, 10000 );
3512     Sleep(100);
3513     CloseHandle( info.wait );
3514 }
3515
3516 static HINTERNET closetest_session, closetest_req, closetest_conn;
3517 static BOOL closetest_closed;
3518
3519 static void WINAPI closetest_callback(HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus,
3520      LPVOID lpvStatusInformation, DWORD dwStatusInformationLength)
3521 {
3522     DWORD len, type;
3523     BOOL res;
3524
3525     trace("closetest_callback %p: %d\n", hInternet, dwInternetStatus);
3526
3527     ok(hInternet == closetest_session || hInternet == closetest_conn || hInternet == closetest_req,
3528        "Unexpected hInternet %p\n", hInternet);
3529     if(!closetest_closed)
3530         return;
3531
3532     len = sizeof(type);
3533     res = InternetQueryOptionA(closetest_req, INTERNET_OPTION_HANDLE_TYPE, &type, &len);
3534     ok(!res && GetLastError() == ERROR_INVALID_HANDLE,
3535        "InternetQueryOptionA(%p INTERNET_OPTION_HANDLE_TYPE) failed: %x %u, expected TRUE ERROR_INVALID_HANDLE\n",
3536        closetest_req, res, GetLastError());
3537 }
3538
3539 static void test_InternetCloseHandle(void)
3540 {
3541     DWORD len, flags;
3542     BOOL res;
3543
3544     closetest_session = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
3545     ok(closetest_session != NULL,"InternetOpen failed with error %u\n", GetLastError());
3546
3547     pInternetSetStatusCallbackA(closetest_session, closetest_callback);
3548
3549     closetest_conn = InternetConnectA(closetest_session, "source.winehq.org", INTERNET_INVALID_PORT_NUMBER,
3550             NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
3551     ok(closetest_conn != NULL,"InternetConnect failed with error %u\n", GetLastError());
3552
3553     closetest_req = HttpOpenRequestA(closetest_conn, "GET", "winegecko.php", NULL, NULL, NULL,
3554             INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE, 0xdeadbead);
3555
3556     res = HttpSendRequestA(closetest_req, NULL, -1, NULL, 0);
3557     ok(!res && (GetLastError() == ERROR_IO_PENDING),
3558        "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
3559
3560     len = sizeof(flags);
3561     res = InternetQueryOptionA(closetest_req, INTERNET_OPTION_REQUEST_FLAGS, &flags, &len);
3562     ok(res, "InternetQueryOptionA(%p INTERNET_OPTION_URL) failed: %u\n", closetest_req, GetLastError());
3563
3564     res = InternetCloseHandle(closetest_session);
3565     ok(res, "InternetCloseHandle failed: %u\n", GetLastError());
3566     closetest_closed = TRUE;
3567     trace("Closed session handle\n");
3568
3569     res = InternetCloseHandle(closetest_conn);
3570     ok(!res && GetLastError() == ERROR_INVALID_HANDLE, "InternetCloseConnection(conn) failed: %x %u\n",
3571        res, GetLastError());
3572
3573     res = InternetCloseHandle(closetest_req);
3574     ok(!res && GetLastError() == ERROR_INVALID_HANDLE, "InternetCloseConnection(req) failed: %x %u\n",
3575        res, GetLastError());
3576
3577     len = sizeof(flags);
3578     res = InternetQueryOptionA(closetest_req, INTERNET_OPTION_REQUEST_FLAGS, &flags, &len);
3579     ok(!res && GetLastError() == ERROR_INVALID_HANDLE,
3580        "InternetQueryOptionA(%p INTERNET_OPTION_URL) failed: %x %u, expected TRUE ERROR_INVALID_HANDLE\n",
3581        closetest_req, res, GetLastError());
3582 }
3583
3584 static void init_status_tests(void)
3585 {
3586     memset(expect, 0, sizeof(expect));
3587     memset(optional, 0, sizeof(optional));
3588     memset(wine_allow, 0, sizeof(wine_allow));
3589     memset(notified, 0, sizeof(notified));
3590     memset(status_string, 0, sizeof(status_string));
3591
3592 #define STATUS_STRING(status) status_string[status] = #status
3593     STATUS_STRING(INTERNET_STATUS_RESOLVING_NAME);
3594     STATUS_STRING(INTERNET_STATUS_NAME_RESOLVED);
3595     STATUS_STRING(INTERNET_STATUS_CONNECTING_TO_SERVER);
3596     STATUS_STRING(INTERNET_STATUS_CONNECTED_TO_SERVER);
3597     STATUS_STRING(INTERNET_STATUS_SENDING_REQUEST);
3598     STATUS_STRING(INTERNET_STATUS_REQUEST_SENT);
3599     STATUS_STRING(INTERNET_STATUS_RECEIVING_RESPONSE);
3600     STATUS_STRING(INTERNET_STATUS_RESPONSE_RECEIVED);
3601     STATUS_STRING(INTERNET_STATUS_CTL_RESPONSE_RECEIVED);
3602     STATUS_STRING(INTERNET_STATUS_PREFETCH);
3603     STATUS_STRING(INTERNET_STATUS_CLOSING_CONNECTION);
3604     STATUS_STRING(INTERNET_STATUS_CONNECTION_CLOSED);
3605     STATUS_STRING(INTERNET_STATUS_HANDLE_CREATED);
3606     STATUS_STRING(INTERNET_STATUS_HANDLE_CLOSING);
3607     STATUS_STRING(INTERNET_STATUS_DETECTING_PROXY);
3608     STATUS_STRING(INTERNET_STATUS_REQUEST_COMPLETE);
3609     STATUS_STRING(INTERNET_STATUS_REDIRECT);
3610     STATUS_STRING(INTERNET_STATUS_INTERMEDIATE_RESPONSE);
3611     STATUS_STRING(INTERNET_STATUS_USER_INPUT_REQUIRED);
3612     STATUS_STRING(INTERNET_STATUS_STATE_CHANGE);
3613     STATUS_STRING(INTERNET_STATUS_COOKIE_SENT);
3614     STATUS_STRING(INTERNET_STATUS_COOKIE_RECEIVED);
3615     STATUS_STRING(INTERNET_STATUS_PRIVACY_IMPACTED);
3616     STATUS_STRING(INTERNET_STATUS_P3P_HEADER);
3617     STATUS_STRING(INTERNET_STATUS_P3P_POLICYREF);
3618     STATUS_STRING(INTERNET_STATUS_COOKIE_HISTORY);
3619 #undef STATUS_STRING
3620 }
3621
3622 START_TEST(http)
3623 {
3624     HMODULE hdll;
3625     hdll = GetModuleHandleA("wininet.dll");
3626
3627     if(!GetProcAddress(hdll, "InternetGetCookieExW")) {
3628         win_skip("Too old IE (older than 6.0)\n");
3629         return;
3630     }
3631
3632     pInternetSetStatusCallbackA = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackA");
3633
3634     init_status_tests();
3635     test_InternetCloseHandle();
3636     InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[0]);
3637     InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[1]);
3638     InternetReadFile_test(0, &test_data[1]);
3639     first_connection_to_test_url = TRUE;
3640     InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[2]);
3641     InternetReadFile_test(0, &test_data[2]);
3642     InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
3643     test_open_url_async();
3644     test_async_HttpSendRequestEx(&notification_data[0]);
3645     test_async_HttpSendRequestEx(&notification_data[1]);
3646     test_async_HttpSendRequestEx(&notification_data[2]);
3647     InternetOpenRequest_test();
3648     test_http_cache();
3649     InternetOpenUrlA_test();
3650     HttpHeaders_test();
3651     test_http_connection();
3652     test_secure_connection();
3653     test_user_agent_header();
3654     test_bogus_accept_types_array();
3655     InternetReadFile_chunked_test();
3656     HttpSendRequestEx_test();
3657     InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[3]);
3658 }