wininet: Make a test pass on IE6.
[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://www.winehq.org/site/about"
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 /* SET_WINE_ALLOW's should be used with an appropriate
47  * todo_wine CHECK_NOTIFIED at a later point in the code */
48 #define SET_WINE_ALLOW2(status, num) \
49     wine_allow[status] = num
50
51 #define SET_WINE_ALLOW(status) \
52     SET_WINE_ALLOW2(status, 1)
53
54 #define CHECK_EXPECT(status) \
55     do { \
56         if (!expect[status] && wine_allow[status]) \
57         { \
58             todo_wine ok(expect[status], "unexpected status %d (%s)\n", status, \
59                          status < MAX_INTERNET_STATUS && status_string[status][0] != 0 ? \
60                          status_string[status] : "unknown");            \
61             wine_allow[status]--; \
62         } \
63         else \
64         { \
65             ok(expect[status], "unexpected status %d (%s)\n", status,   \
66                status < MAX_INTERNET_STATUS && status_string[status][0] != 0 ? \
67                status_string[status] : "unknown");                      \
68             expect[status]--;                                           \
69         } \
70         notified[status]++; \
71     }while(0)
72
73 /* CLEAR_NOTIFIED used in cases when notification behavior
74  * differs between Windows versions */
75 #define CLEAR_NOTIFIED(status) \
76     expect[status] = wine_allow[status] = notified[status] = 0;
77
78 #define CHECK_NOTIFIED2(status, num) \
79     do { \
80         ok(notified[status] == (num), "expected status %d (%s) %d times, received %d times\n", \
81            status, status < MAX_INTERNET_STATUS && status_string[status][0] != 0 ? \
82            status_string[status] : "unknown", (num), notified[status]); \
83         CLEAR_NOTIFIED(status);                                         \
84     }while(0)
85
86 #define CHECK_NOTIFIED(status) \
87     CHECK_NOTIFIED2(status, 1)
88
89 #define CHECK_NOT_NOTIFIED(status) \
90     CHECK_NOTIFIED2(status, 0)
91
92 #define MAX_INTERNET_STATUS (INTERNET_STATUS_COOKIE_HISTORY+1)
93 #define MAX_STATUS_NAME 50
94 static int expect[MAX_INTERNET_STATUS], wine_allow[MAX_INTERNET_STATUS],
95     notified[MAX_INTERNET_STATUS];
96 static CHAR status_string[MAX_INTERNET_STATUS][MAX_STATUS_NAME];
97
98 static HANDLE hCompleteEvent;
99
100 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET ,INTERNET_STATUS_CALLBACK);
101 static BOOL (WINAPI *pInternetTimeFromSystemTimeA)(CONST SYSTEMTIME *,DWORD ,LPSTR ,DWORD);
102 static BOOL (WINAPI *pInternetTimeFromSystemTimeW)(CONST SYSTEMTIME *,DWORD ,LPWSTR ,DWORD);
103 static BOOL (WINAPI *pInternetTimeToSystemTimeA)(LPCSTR ,SYSTEMTIME *,DWORD);
104 static BOOL (WINAPI *pInternetTimeToSystemTimeW)(LPCWSTR ,SYSTEMTIME *,DWORD);
105
106
107 static VOID WINAPI callback(
108      HINTERNET hInternet,
109      DWORD_PTR dwContext,
110      DWORD dwInternetStatus,
111      LPVOID lpvStatusInformation,
112      DWORD dwStatusInformationLength
113 )
114 {
115     CHECK_EXPECT(dwInternetStatus);
116     switch (dwInternetStatus)
117     {
118         case INTERNET_STATUS_RESOLVING_NAME:
119             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESOLVING_NAME \"%s\" %d\n",
120                 GetCurrentThreadId(), hInternet, dwContext,
121                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
122             *(LPSTR)lpvStatusInformation = '\0';
123             break;
124         case INTERNET_STATUS_NAME_RESOLVED:
125             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_NAME_RESOLVED \"%s\" %d\n",
126                 GetCurrentThreadId(), hInternet, dwContext,
127                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
128             *(LPSTR)lpvStatusInformation = '\0';
129             break;
130         case INTERNET_STATUS_CONNECTING_TO_SERVER:
131             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %d\n",
132                 GetCurrentThreadId(), hInternet, dwContext,
133                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
134             *(LPSTR)lpvStatusInformation = '\0';
135             break;
136         case INTERNET_STATUS_CONNECTED_TO_SERVER:
137             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %d\n",
138                 GetCurrentThreadId(), hInternet, dwContext,
139                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
140             *(LPSTR)lpvStatusInformation = '\0';
141             break;
142         case INTERNET_STATUS_SENDING_REQUEST:
143             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_SENDING_REQUEST %p %d\n",
144                 GetCurrentThreadId(), hInternet, dwContext,
145                 lpvStatusInformation,dwStatusInformationLength);
146             break;
147         case INTERNET_STATUS_REQUEST_SENT:
148             ok(dwStatusInformationLength == sizeof(DWORD),
149                 "info length should be sizeof(DWORD) instead of %d\n",
150                 dwStatusInformationLength);
151             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_SENT 0x%x %d\n",
152                 GetCurrentThreadId(), hInternet, dwContext,
153                 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
154             break;
155         case INTERNET_STATUS_RECEIVING_RESPONSE:
156             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RECEIVING_RESPONSE %p %d\n",
157                 GetCurrentThreadId(), hInternet, dwContext,
158                 lpvStatusInformation,dwStatusInformationLength);
159             break;
160         case INTERNET_STATUS_RESPONSE_RECEIVED:
161             ok(dwStatusInformationLength == sizeof(DWORD),
162                 "info length should be sizeof(DWORD) instead of %d\n",
163                 dwStatusInformationLength);
164             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESPONSE_RECEIVED 0x%x %d\n",
165                 GetCurrentThreadId(), hInternet, dwContext,
166                 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
167             break;
168         case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
169             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %d\n",
170                 GetCurrentThreadId(), hInternet,dwContext,
171                 lpvStatusInformation,dwStatusInformationLength);
172             break;
173         case INTERNET_STATUS_PREFETCH:
174             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_PREFETCH %p %d\n",
175                 GetCurrentThreadId(), hInternet, dwContext,
176                 lpvStatusInformation,dwStatusInformationLength);
177             break;
178         case INTERNET_STATUS_CLOSING_CONNECTION:
179             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CLOSING_CONNECTION %p %d\n",
180                 GetCurrentThreadId(), hInternet, dwContext,
181                 lpvStatusInformation,dwStatusInformationLength);
182             break;
183         case INTERNET_STATUS_CONNECTION_CLOSED:
184             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTION_CLOSED %p %d\n",
185                 GetCurrentThreadId(), hInternet, dwContext,
186                 lpvStatusInformation,dwStatusInformationLength);
187             break;
188         case INTERNET_STATUS_HANDLE_CREATED:
189             ok(dwStatusInformationLength == sizeof(HINTERNET),
190                 "info length should be sizeof(HINTERNET) instead of %d\n",
191                 dwStatusInformationLength);
192             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CREATED %p %d\n",
193                 GetCurrentThreadId(), hInternet, dwContext,
194                 *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
195             CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
196             SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
197             break;
198         case INTERNET_STATUS_HANDLE_CLOSING:
199             ok(dwStatusInformationLength == sizeof(HINTERNET),
200                 "info length should be sizeof(HINTERNET) instead of %d\n",
201                 dwStatusInformationLength);
202             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
203                 GetCurrentThreadId(), hInternet, dwContext,
204                 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
205             break;
206         case INTERNET_STATUS_REQUEST_COMPLETE:
207         {
208             INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
209             ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
210                 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
211                 dwStatusInformationLength);
212             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%ld,%d} %d\n",
213                 GetCurrentThreadId(), hInternet, dwContext,
214                 iar->dwResult,iar->dwError,dwStatusInformationLength);
215             SetEvent(hCompleteEvent);
216             break;
217         }
218         case INTERNET_STATUS_REDIRECT:
219             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
220                 GetCurrentThreadId(), hInternet, dwContext,
221                 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
222             *(LPSTR)lpvStatusInformation = '\0';
223             CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
224             SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
225             break;
226         case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
227             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
228                 GetCurrentThreadId(), hInternet, dwContext,
229                 lpvStatusInformation, dwStatusInformationLength);
230             break;
231         default:
232             trace("%04x:Callback %p 0x%lx %d %p %d\n",
233                 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
234                 lpvStatusInformation, dwStatusInformationLength);
235     }
236 }
237
238 static void InternetReadFile_test(int flags)
239 {
240     BOOL res;
241     DWORD rc;
242     CHAR buffer[4000];
243     DWORD length;
244     DWORD out;
245     const char *types[2] = { "*", NULL };
246     HINTERNET hi, hic = 0, hor = 0;
247
248     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
249
250     trace("Starting InternetReadFile test with flags 0x%x\n",flags);
251
252     trace("InternetOpenA <--\n");
253     hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
254     ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
255     trace("InternetOpenA -->\n");
256
257     if (hi == 0x0) goto abort;
258
259     pInternetSetStatusCallbackA(hi,&callback);
260
261     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
262
263     trace("InternetConnectA <--\n");
264     hic=InternetConnectA(hi, "www.winehq.org", INTERNET_INVALID_PORT_NUMBER,
265                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
266     ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
267     trace("InternetConnectA -->\n");
268
269     if (hic == 0x0) goto abort;
270
271     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
272     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
273
274     trace("HttpOpenRequestA <--\n");
275     hor = HttpOpenRequestA(hic, "GET", "/about/", NULL, NULL, types,
276                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
277                            0xdeadbead);
278     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
279         /*
280          * If the internet name can't be resolved we are probably behind
281          * a firewall or in some other way not directly connected to the
282          * Internet. Not enough reason to fail the test. Just ignore and
283          * abort.
284          */
285     } else  {
286         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
287     }
288     trace("HttpOpenRequestA -->\n");
289
290     if (hor == 0x0) goto abort;
291
292     length = sizeof(buffer);
293     res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
294     ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
295     ok(!strcmp(buffer, "http://www.winehq.org/about/"), "Wrong URL %s\n", buffer);
296
297     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
298     CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
299     CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
300     if (first_connection_to_test_url)
301     {
302         SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
303         SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
304     }
305     else
306     {
307         SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
308         SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
309     }
310     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
311     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
312     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
313     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
314     SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
315     SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
316     SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
317     SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
318     SET_EXPECT(INTERNET_STATUS_REDIRECT);
319     if (flags & INTERNET_FLAG_ASYNC)
320         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
321     else
322         SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
323
324     trace("HttpSendRequestA -->\n");
325     SetLastError(0xdeadbeef);
326     rc = HttpSendRequestA(hor, "", -1, NULL, 0);
327     if (flags & INTERNET_FLAG_ASYNC)
328         ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
329             "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
330     else
331         ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
332            "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
333     trace("HttpSendRequestA <--\n");
334
335     if (flags & INTERNET_FLAG_ASYNC)
336         WaitForSingleObject(hCompleteEvent, INFINITE);
337
338     if (first_connection_to_test_url)
339     {
340         CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
341         CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
342     }
343     else todo_wine
344     {
345         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
346         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
347     }
348     CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
349     CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
350     CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
351     CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
352     CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
353     if (flags & INTERNET_FLAG_ASYNC)
354         CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
355     else
356         todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
357     /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
358     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
359     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
360
361     length = 4;
362     rc = InternetQueryOptionA(hor,INTERNET_OPTION_REQUEST_FLAGS,&out,&length);
363     trace("Option 0x17 -> %i  %i\n",rc,out);
364
365     length = 100;
366     rc = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
367     trace("Option 0x22 -> %i  %s\n",rc,buffer);
368
369     length = 4000;
370     rc = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
371     buffer[length]=0;
372     trace("Option 0x16 -> %i  %s\n",rc,buffer);
373
374     length = sizeof(buffer);
375     res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
376     ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
377     ok(!strcmp(buffer, "http://www.winehq.org/site/about"), "Wrong URL %s\n", buffer);
378
379     length = 16;
380     rc = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
381     trace("Option 0x5 -> %i  %s  (%u)\n",rc,buffer,GetLastError());
382
383     length = 100;
384     rc = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
385     buffer[length]=0;
386     trace("Option 0x1 -> %i  %s\n",rc,buffer);
387
388     SetLastError(0xdeadbeef);
389     rc = InternetReadFile(NULL, buffer, 100, &length);
390     ok(!rc, "InternetReadFile should have failed\n");
391     ok(GetLastError() == ERROR_INVALID_HANDLE,
392         "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
393         GetLastError());
394
395     length = 100;
396     trace("Entering Query loop\n");
397
398     SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
399     SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
400     while (TRUE)
401     {
402         if (flags & INTERNET_FLAG_ASYNC)
403             SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
404         rc = InternetQueryDataAvailable(hor,&length,0x0,0x0);
405         ok(!(rc == 0 && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
406         ok(rc != 0 || ((flags & INTERNET_FLAG_ASYNC) && GetLastError() == ERROR_IO_PENDING),
407            "InternetQueryDataAvailable failed, error %d\n", GetLastError());
408         if (flags & INTERNET_FLAG_ASYNC)
409         {
410             if (rc != 0)
411             {
412                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
413             }
414             else if (GetLastError() == ERROR_IO_PENDING)
415             {
416                 WaitForSingleObject(hCompleteEvent, INFINITE);
417                 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
418                 continue;
419             }
420         }
421         if (length)
422         {
423             char *buffer;
424             buffer = HeapAlloc(GetProcessHeap(),0,length+1);
425
426             rc = InternetReadFile(hor,buffer,length,&length);
427
428             buffer[length]=0;
429
430             trace("ReadFile -> %i %i\n",rc,length);
431
432             HeapFree(GetProcessHeap(),0,buffer);
433         }
434         if (length == 0)
435             break;
436     }
437     /* WinXP does not send, but Win98 does */
438     CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
439     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
440 abort:
441     SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
442     if (hor != 0x0) {
443         SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
444         SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
445         SetLastError(0xdeadbeef);
446         rc = InternetCloseHandle(hor);
447         ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
448         SetLastError(0xdeadbeef);
449         rc = InternetCloseHandle(hor);
450         ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
451         ok (GetLastError() == ERROR_INVALID_HANDLE,
452             "Double close of handle should have set ERROR_INVALID_HANDLE instead of %u\n",
453             GetLastError());
454     }
455     /* We intentionally do not close the handle opened by InternetConnectA as this
456      * tickles bug #9479: native closes child internet handles when the parent handles
457      * are closed. This is verified below by checking that the number of
458      * INTERNET_STATUS_HANDLE_CLOSING notifications matches the number expected. */
459     if (hi != 0x0) {
460       SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
461       rc = InternetCloseHandle(hi);
462       ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
463       if (flags & INTERNET_FLAG_ASYNC)
464           Sleep(100);
465     }
466     CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
467     if (hor != 0x0) todo_wine
468     {
469         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
470         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
471     }
472     else
473     {
474         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
475         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
476     }
477     CloseHandle(hCompleteEvent);
478     first_connection_to_test_url = FALSE;
479 }
480
481 static void InternetReadFileExA_test(int flags)
482 {
483     DWORD rc;
484     DWORD length;
485     const char *types[2] = { "*", NULL };
486     HINTERNET hi, hic = 0, hor = 0;
487     INTERNET_BUFFERS inetbuffers;
488
489     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
490
491     trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
492
493     trace("InternetOpenA <--\n");
494     hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
495     ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
496     trace("InternetOpenA -->\n");
497
498     if (hi == 0x0) goto abort;
499
500     pInternetSetStatusCallbackA(hi,&callback);
501
502     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
503
504     trace("InternetConnectA <--\n");
505     hic=InternetConnectA(hi, "www.winehq.org", INTERNET_INVALID_PORT_NUMBER,
506                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
507     ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
508     trace("InternetConnectA -->\n");
509
510     if (hic == 0x0) goto abort;
511
512     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
513     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
514
515     trace("HttpOpenRequestA <--\n");
516     hor = HttpOpenRequestA(hic, "GET", "/about/", NULL, NULL, types,
517                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
518                            0xdeadbead);
519     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
520         /*
521          * If the internet name can't be resolved we are probably behind
522          * a firewall or in some other way not directly connected to the
523          * Internet. Not enough reason to fail the test. Just ignore and
524          * abort.
525          */
526     } else  {
527         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
528     }
529     trace("HttpOpenRequestA -->\n");
530
531     if (hor == 0x0) goto abort;
532
533     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
534     CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
535     CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
536     if (first_connection_to_test_url)
537     {
538         SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
539         SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
540     }
541     else
542     {
543         SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
544         SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
545     }
546     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
547     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
548     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
549     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
550     SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
551     SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
552     SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
553     SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
554     SET_EXPECT(INTERNET_STATUS_REDIRECT);
555     if (flags & INTERNET_FLAG_ASYNC)
556         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
557     else
558         SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
559
560     trace("HttpSendRequestA -->\n");
561     SetLastError(0xdeadbeef);
562     rc = HttpSendRequestA(hor, "", -1, NULL, 0);
563     if (flags & INTERNET_FLAG_ASYNC)
564         ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
565             "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
566     else
567         ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
568            "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
569     trace("HttpSendRequestA <--\n");
570
571     if (!rc && (GetLastError() == ERROR_IO_PENDING))
572         WaitForSingleObject(hCompleteEvent, INFINITE);
573
574     if (first_connection_to_test_url)
575     {
576         CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
577         CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
578     }
579     else todo_wine
580     {
581         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
582         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
583     }
584     CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
585     CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
586     CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
587     CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
588     CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
589     if (flags & INTERNET_FLAG_ASYNC)
590         CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
591     else
592         todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
593     /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
594     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
595     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
596
597     /* tests invalid dwStructSize */
598     inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS)+1;
599     inetbuffers.lpcszHeader = NULL;
600     inetbuffers.dwHeadersLength = 0;
601     inetbuffers.dwBufferLength = 10;
602     inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
603     inetbuffers.dwOffsetHigh = 1234;
604     inetbuffers.dwOffsetLow = 5678;
605     rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
606     ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
607         "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
608         rc ? "TRUE" : "FALSE", GetLastError());
609     HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
610
611     /* tests to see whether lpcszHeader is used - it isn't */
612     inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
613     inetbuffers.lpcszHeader = (LPCTSTR)0xdeadbeef;
614     inetbuffers.dwHeadersLength = 255;
615     inetbuffers.dwBufferLength = 0;
616     inetbuffers.lpvBuffer = NULL;
617     inetbuffers.dwOffsetHigh = 1234;
618     inetbuffers.dwOffsetLow = 5678;
619     SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
620     SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
621     rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
622     ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
623     todo_wine
624     {
625         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
626         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
627     }
628
629     rc = InternetReadFileEx(NULL, &inetbuffers, 0, 0xdeadcafe);
630     ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
631         "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
632         rc ? "TRUE" : "FALSE", GetLastError());
633
634     length = 0;
635     trace("Entering Query loop\n");
636
637     SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
638     SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
639     while (TRUE)
640     {
641         inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
642         inetbuffers.dwBufferLength = 1024;
643         inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
644         inetbuffers.dwOffsetHigh = 1234;
645         inetbuffers.dwOffsetLow = 5678;
646
647         SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
648         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
649         SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
650         rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
651         if (!rc)
652         {
653             if (GetLastError() == ERROR_IO_PENDING)
654             {
655                 trace("InternetReadFileEx -> PENDING\n");
656                 ok(flags & INTERNET_FLAG_ASYNC,
657                    "Should not get ERROR_IO_PENDING without INTERNET_FLAG_ASYNC\n");
658                 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
659                 WaitForSingleObject(hCompleteEvent, INFINITE);
660                 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
661                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
662             }
663             else
664             {
665                 trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
666                 break;
667             }
668         }
669         else
670         {
671             trace("InternetReadFileEx -> SUCCEEDED\n");
672             CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
673             if (inetbuffers.dwBufferLength)
674             {
675                 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
676                 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
677             }
678             else
679             {
680                 /* Win98 still sends these when 0 bytes are read, WinXP does not */
681                 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
682                 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
683             }
684         }
685
686         trace("read %i bytes\n", inetbuffers.dwBufferLength);
687         ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
688
689         ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
690             "InternetReadFileEx sets offsets to 0x%x%08x\n",
691             inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
692
693         HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
694
695         if (!inetbuffers.dwBufferLength)
696             break;
697
698         length += inetbuffers.dwBufferLength;
699     }
700     ok(length > 0, "failed to read any of the document\n");
701     trace("Finished. Read %d bytes\n", length);
702
703     /* WinXP does not send, but Win98 does */
704     CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
705     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
706 abort:
707     SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
708     if (hor) {
709         SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
710         SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
711         rc = InternetCloseHandle(hor);
712         ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
713         rc = InternetCloseHandle(hor);
714         ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
715     }
716     if (hic) {
717         rc = InternetCloseHandle(hic);
718         ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed\n");
719     }
720     if (hi) {
721       SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
722       rc = InternetCloseHandle(hi);
723       ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
724       if (flags & INTERNET_FLAG_ASYNC)
725           Sleep(100);
726       CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
727     }
728     if (hor != 0x0) todo_wine
729     {
730         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
731         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
732     }
733     else
734     {
735         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
736         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
737     }
738     CloseHandle(hCompleteEvent);
739     first_connection_to_test_url = FALSE;
740 }
741
742 static void InternetOpenUrlA_test(void)
743 {
744   HINTERNET myhinternet, myhttp;
745   char buffer[0x400];
746   DWORD size, readbytes, totalbytes=0;
747   BOOL ret;
748   
749   myhinternet = InternetOpen("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
750   ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
751   size = 0x400;
752   ret = InternetCanonicalizeUrl(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
753   ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
754
755   SetLastError(0);
756   myhttp = InternetOpenUrl(myhinternet, TEST_URL, 0, 0,
757                            INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
758   if (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
759     return; /* WinXP returns this when not connected to the net */
760   ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
761   ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
762   ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
763   totalbytes += readbytes;
764   while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
765     totalbytes += readbytes;
766   trace("read 0x%08x bytes\n",totalbytes);
767
768   InternetCloseHandle(myhttp);
769   InternetCloseHandle(myhinternet);
770 }
771
772 static void InternetTimeFromSystemTimeA_test(void)
773 {
774     BOOL ret;
775     static const SYSTEMTIME time = { 2005, 1, 5, 7, 12, 6, 35, 0 };
776     char string[INTERNET_RFC1123_BUFSIZE];
777     static const char expect[] = "Fri, 07 Jan 2005 12:06:35 GMT";
778
779     ret = pInternetTimeFromSystemTimeA( &time, INTERNET_RFC1123_FORMAT, string, sizeof(string) );
780     ok( ret, "InternetTimeFromSystemTimeA failed (%u)\n", GetLastError() );
781
782     ok( !memcmp( string, expect, sizeof(expect) ),
783         "InternetTimeFromSystemTimeA failed (%u)\n", GetLastError() );
784 }
785
786 static void InternetTimeFromSystemTimeW_test(void)
787 {
788     BOOL ret;
789     static const SYSTEMTIME time = { 2005, 1, 5, 7, 12, 6, 35, 0 };
790     WCHAR string[INTERNET_RFC1123_BUFSIZE + 1];
791     static const WCHAR expect[] = { 'F','r','i',',',' ','0','7',' ','J','a','n',' ','2','0','0','5',' ',
792                                     '1','2',':','0','6',':','3','5',' ','G','M','T',0 };
793
794     ret = pInternetTimeFromSystemTimeW( &time, INTERNET_RFC1123_FORMAT, string, sizeof(string) );
795     ok( ret, "InternetTimeFromSystemTimeW failed (%u)\n", GetLastError() );
796
797     ok( !memcmp( string, expect, sizeof(expect) ),
798         "InternetTimeFromSystemTimeW failed (%u)\n", GetLastError() );
799 }
800
801 static void InternetTimeToSystemTimeA_test(void)
802 {
803     BOOL ret;
804     SYSTEMTIME time;
805     static const SYSTEMTIME expect = { 2005, 1, 5, 7, 12, 6, 35, 0 };
806     static const char string[] = "Fri, 07 Jan 2005 12:06:35 GMT";
807     static const char string2[] = " fri 7 jan 2005 12 06 35";
808
809     ret = pInternetTimeToSystemTimeA( string, &time, 0 );
810     ok( ret, "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
811     ok( !memcmp( &time, &expect, sizeof(expect) ),
812         "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
813
814     ret = pInternetTimeToSystemTimeA( string2, &time, 0 );
815     ok( ret, "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
816     ok( !memcmp( &time, &expect, sizeof(expect) ),
817         "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
818 }
819
820 static void InternetTimeToSystemTimeW_test(void)
821 {
822     BOOL ret;
823     SYSTEMTIME time;
824     static const SYSTEMTIME expect = { 2005, 1, 5, 7, 12, 6, 35, 0 };
825     static const WCHAR string[] = { 'F','r','i',',',' ','0','7',' ','J','a','n',' ','2','0','0','5',' ',
826                                     '1','2',':','0','6',':','3','5',' ','G','M','T',0 };
827     static const WCHAR string2[] = { ' ','f','r','i',' ','7',' ','j','a','n',' ','2','0','0','5',' ',
828                                      '1','2',' ','0','6',' ','3','5',0 };
829     static const WCHAR string3[] = { 'F','r',0 };
830
831     ret = pInternetTimeToSystemTimeW( NULL, NULL, 0 );
832     ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
833
834     ret = pInternetTimeToSystemTimeW( NULL, &time, 0 );
835     ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
836
837     ret = pInternetTimeToSystemTimeW( string, NULL, 0 );
838     ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
839
840     ret = pInternetTimeToSystemTimeW( string, &time, 0 );
841     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
842
843     ret = pInternetTimeToSystemTimeW( string, &time, 0 );
844     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
845     ok( !memcmp( &time, &expect, sizeof(expect) ),
846         "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
847
848     ret = pInternetTimeToSystemTimeW( string2, &time, 0 );
849     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
850     ok( !memcmp( &time, &expect, sizeof(expect) ),
851         "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
852
853     ret = pInternetTimeToSystemTimeW( string3, &time, 0 );
854     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
855 }
856
857 static void HttpSendRequestEx_test(void)
858 {
859     HINTERNET hSession;
860     HINTERNET hConnect;
861     HINTERNET hRequest;
862
863     INTERNET_BUFFERS BufferIn;
864     DWORD dwBytesWritten;
865     DWORD dwBytesRead;
866     CHAR szBuffer[256];
867     int i;
868     BOOL ret;
869
870     static char szPostData[] = "mode=Test";
871     static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
872
873     hSession = InternetOpen("Wine Regression Test",
874             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
875     ok( hSession != NULL ,"Unable to open Internet session\n");
876     hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
877             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
878             0);
879     ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
880     hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
881             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
882     if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
883     {
884         trace( "Network unreachable, skipping test\n" );
885         goto done;
886     }
887     ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
888
889
890     BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS);
891     BufferIn.Next = (LPINTERNET_BUFFERS)0xdeadcab;
892     BufferIn.lpcszHeader = szContentType;
893     BufferIn.dwHeadersLength = sizeof(szContentType)-1;
894     BufferIn.dwHeadersTotal = sizeof(szContentType)-1;
895     BufferIn.lpvBuffer = szPostData;
896     BufferIn.dwBufferLength = 3;
897     BufferIn.dwBufferTotal = sizeof(szPostData)-1;
898     BufferIn.dwOffsetLow = 0;
899     BufferIn.dwOffsetHigh = 0;
900
901     ret = HttpSendRequestEx(hRequest, &BufferIn, NULL, 0 ,0);
902     ok(ret, "HttpSendRequestEx Failed with error %u\n", GetLastError());
903
904     for (i = 3; szPostData[i]; i++)
905         ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
906                 "InternetWriteFile failed\n");
907
908     ok(HttpEndRequest(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
909
910     ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
911             "Unable to read response\n");
912     szBuffer[dwBytesRead] = 0;
913
914     ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
915     ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0,"Got string %s\n",szBuffer);
916
917     ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
918 done:
919     ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
920     ok(InternetCloseHandle(hSession), "Close session handle failed\n");
921 }
922
923 static void InternetOpenRequest_test(void)
924 {
925     HINTERNET session, connect, request;
926     static const char *types[] = { "*", "", NULL };
927     static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
928     static const WCHAR *typesW[] = { any, empty, NULL };
929     BOOL ret;
930
931     session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
932     ok(session != NULL ,"Unable to open Internet session\n");
933
934     connect = InternetConnectA(session, NULL, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
935                               INTERNET_SERVICE_HTTP, 0, 0);
936     ok(connect == NULL, "InternetConnectA should have failed\n");
937     ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
938
939     connect = InternetConnectA(session, "", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
940                               INTERNET_SERVICE_HTTP, 0, 0);
941     ok(connect == NULL, "InternetConnectA should have failed\n");
942     ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
943
944     connect = InternetConnectA(session, "winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
945                               INTERNET_SERVICE_HTTP, 0, 0);
946     ok(connect != NULL, "Unable to connect to http://winehq.org with error %d\n", GetLastError());
947
948     request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
949     if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
950     {
951         trace( "Network unreachable, skipping test\n" );
952         goto done;
953     }
954     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
955
956     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
957     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
958     ok(InternetCloseHandle(request), "Close request handle failed\n");
959
960     request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
961     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
962
963     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
964     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
965     ok(InternetCloseHandle(request), "Close request handle failed\n");
966
967 done:
968     ok(InternetCloseHandle(connect), "Close connect handle failed\n");
969     ok(InternetCloseHandle(session), "Close session handle failed\n");
970 }
971
972 static void test_http_cache(void)
973 {
974     HINTERNET session, connect, request;
975     char file_name[MAX_PATH], url[INTERNET_MAX_URL_LENGTH];
976     DWORD size, file_size;
977     BYTE buf[100];
978     HANDLE file;
979     BOOL ret;
980
981     static const char *types[] = { "*", "", NULL };
982
983     session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
984     ok(session != NULL ,"Unable to open Internet session\n");
985
986     connect = InternetConnectA(session, "www.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
987                               INTERNET_SERVICE_HTTP, 0, 0);
988     ok(connect != NULL, "Unable to connect to http://winehq.org with error %d\n", GetLastError());
989
990     request = HttpOpenRequestA(connect, NULL, "/site/about", NULL, NULL, types, INTERNET_FLAG_NEED_FILE, 0);
991     if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
992     {
993         trace( "Network unreachable, skipping test\n" );
994
995         ok(InternetCloseHandle(connect), "Close connect handle failed\n");
996         ok(InternetCloseHandle(session), "Close session handle failed\n");
997
998         return;
999     }
1000     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1001
1002     size = sizeof(url);
1003     ret = InternetQueryOptionA(request, INTERNET_OPTION_URL, url, &size);
1004     ok(ret, "InternetQueryOptionA(INTERNET_OPTION_url) failed: %u\n", GetLastError());
1005     ok(!strcmp(url, "http://www.winehq.org/site/about"), "Wrong URL %s\n", url);
1006
1007     size = sizeof(file_name);
1008     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1009     ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1010     ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1011     ok(!size, "size = %d\n", size);
1012
1013     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1014     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1015
1016     size = sizeof(file_name);
1017     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1018     ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1019
1020     file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1021                       FILE_ATTRIBUTE_NORMAL, NULL);
1022     ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1023     file_size = GetFileSize(file, NULL);
1024     ok(file_size == 0, "file size=%d\n", file_size);
1025
1026     ret = InternetReadFile(request, buf, sizeof(buf), &size);
1027     ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1028     ok(size == sizeof(buf), "size=%d\n", size);
1029
1030     file_size = GetFileSize(file, NULL);
1031     ok(file_size == sizeof(buf), "file size=%d\n", file_size);
1032     CloseHandle(file);
1033
1034     ok(InternetCloseHandle(request), "Close request handle failed\n");
1035
1036     file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1037                       FILE_ATTRIBUTE_NORMAL, NULL);
1038     ok(file == INVALID_HANDLE_VALUE, "CreateFile succeeded\n");
1039     ok(GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError()=%u, expected ERROR_FILE_NOT_FOUND\n", GetLastError());
1040
1041     request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1042     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1043
1044     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1045     ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1046     ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1047     ok(!size, "size = %d\n", size);
1048
1049     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1050     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1051
1052     size = sizeof(file_name);
1053     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1054     ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1055     ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1056     ok(!size, "size = %d\n", size);
1057
1058     file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1059                       FILE_ATTRIBUTE_NORMAL, NULL);
1060     ok(file == INVALID_HANDLE_VALUE, "CreateFile succeeded\n");
1061     ok(GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError()=%u, expected ERROR_FILE_NOT_FOUND\n", GetLastError());
1062
1063     ok(InternetCloseHandle(request), "Close request handle failed\n");
1064
1065     ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1066     ok(InternetCloseHandle(session), "Close session handle failed\n");
1067 }
1068
1069 static void HttpHeaders_test(void)
1070 {
1071     HINTERNET hSession;
1072     HINTERNET hConnect;
1073     HINTERNET hRequest;
1074     CHAR      buffer[256];
1075     DWORD     len = 256;
1076     DWORD     index = 0;
1077
1078     hSession = InternetOpen("Wine Regression Test",
1079             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1080     ok( hSession != NULL ,"Unable to open Internet session\n");
1081     hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
1082             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1083             0);
1084     ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
1085     hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
1086             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1087     if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1088     {
1089         trace( "Network unreachable, skipping test\n" );
1090         goto done;
1091     }
1092     ok( hRequest != NULL, "Failed to open request handle\n");
1093
1094     index = 0;
1095     len = sizeof(buffer);
1096     strcpy(buffer,"Warning");
1097     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1098                buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1099     
1100     ok(HttpAddRequestHeaders(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1101             "Failed to add new header\n");
1102
1103     index = 0;
1104     len = sizeof(buffer);
1105     strcpy(buffer,"Warning");
1106     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1107                 buffer,&len,&index),"Unable to query header\n");
1108     ok(index == 1, "Index was not incremented\n");
1109     ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1110     ok(len == 5, "Invalid length (exp. 5, got %d)\n", len);
1111     ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1112     len = sizeof(buffer);
1113     strcpy(buffer,"Warning");
1114     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1115                 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1116
1117     index = 0;
1118     len = 5; /* could store the string but not the NULL terminator */
1119     strcpy(buffer,"Warning");
1120     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1121                 buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1122     ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1123     ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
1124
1125     /* a call with NULL will fail but will return the length */
1126     index = 0;
1127     len = sizeof(buffer);
1128     SetLastError(0xdeadbeef);
1129     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1130                 NULL,&len,&index) == FALSE,"Query worked\n");
1131     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1132     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1133     ok(index == 0, "Index was incremented\n");
1134
1135     /* even for a len that is too small */
1136     index = 0;
1137     len = 15;
1138     SetLastError(0xdeadbeef);
1139     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1140                 NULL,&len,&index) == FALSE,"Query worked\n");
1141     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1142     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1143     ok(index == 0, "Index was incremented\n");
1144
1145     index = 0;
1146     len = 0;
1147     SetLastError(0xdeadbeef);
1148     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1149                 NULL,&len,&index) == FALSE,"Query worked\n");
1150     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1151     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1152     ok(index == 0, "Index was incremented\n");
1153
1154
1155     /* a working query */
1156     index = 0;
1157     len = sizeof(buffer);
1158     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1159                 buffer,&len,&index),"Unable to query header\n");
1160     /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1161     ok(strncmp(buffer, "POST /posttest.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1162     ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1163     ok(index == 0, "Index was incremented\n");
1164
1165
1166
1167     ok(HttpAddRequestHeaders(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1168             "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1169
1170     index = 0;
1171     len = sizeof(buffer);
1172     strcpy(buffer,"Warning");
1173     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1174                 buffer,&len,&index),"Unable to query header\n");
1175     ok(index == 1, "Index was not incremented\n");
1176     ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1177     len = sizeof(buffer);
1178     strcpy(buffer,"Warning");
1179     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1180                 buffer,&len,&index),"Failed to get second header\n");
1181     ok(index == 2, "Index was not incremented\n");
1182     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1183     len = sizeof(buffer);
1184     strcpy(buffer,"Warning");
1185     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1186                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1187
1188     ok(HttpAddRequestHeaders(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1189
1190     index = 0;
1191     len = sizeof(buffer);
1192     strcpy(buffer,"Warning");
1193     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1194                 buffer,&len,&index),"Unable to query header\n");
1195     ok(index == 1, "Index was not incremented\n");
1196     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1197     len = sizeof(buffer);
1198     strcpy(buffer,"Warning");
1199     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1200                 buffer,&len,&index),"Failed to get second header\n");
1201     ok(index == 2, "Index was not incremented\n");
1202     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1203     len = sizeof(buffer);
1204     strcpy(buffer,"Warning");
1205     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1206                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1207     
1208     ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1209
1210     index = 0;
1211     len = sizeof(buffer);
1212     strcpy(buffer,"Warning");
1213     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1214                 buffer,&len,&index),"Unable to query header\n");
1215     ok(index == 1, "Index was not incremented\n");
1216     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1217     len = sizeof(buffer);
1218     strcpy(buffer,"Warning");
1219     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1220                 buffer,&len,&index),"Failed to get second header\n");
1221     ok(index == 2, "Index was not incremented\n");
1222     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1223     len = sizeof(buffer);
1224     strcpy(buffer,"Warning");
1225     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1226                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1227
1228     ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1229
1230     index = 0;
1231     len = sizeof(buffer);
1232     strcpy(buffer,"Warning");
1233     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1234                 buffer,&len,&index),"Unable to query header\n");
1235     ok(index == 1, "Index was not incremented\n");
1236     ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
1237     len = sizeof(buffer);
1238     strcpy(buffer,"Warning");
1239     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1240     ok(index == 2, "Index was not incremented\n");
1241     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1242     len = sizeof(buffer);
1243     strcpy(buffer,"Warning");
1244     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1245
1246     ok(HttpAddRequestHeaders(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1247
1248     index = 0;
1249     len = sizeof(buffer);
1250     strcpy(buffer,"Warning");
1251     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1252     ok(index == 1, "Index was not incremented\n");
1253     ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
1254     len = sizeof(buffer);
1255     strcpy(buffer,"Warning");
1256     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1257     ok(index == 2, "Index was not incremented\n");
1258     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1259     len = sizeof(buffer);
1260     strcpy(buffer,"Warning");
1261     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1262
1263     ok(HttpAddRequestHeaders(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1264
1265     index = 0;
1266     len = sizeof(buffer);
1267     strcpy(buffer,"Warning");
1268     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1269     ok(index == 1, "Index was not incremented\n");
1270     ok(strcmp(buffer,"test2, test4, test5; test6")==0, "incorrect string was returned(%s)\n",buffer);
1271     len = sizeof(buffer);
1272     strcpy(buffer,"Warning");
1273     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1274     ok(index == 2, "Index was not incremented\n");
1275     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1276     len = sizeof(buffer);
1277     strcpy(buffer,"Warning");
1278     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1279
1280     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");
1281
1282     index = 0;
1283     len = sizeof(buffer);
1284     strcpy(buffer,"Warning");
1285     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1286     ok(index == 1, "Index was not incremented\n");
1287     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1288     len = sizeof(buffer);
1289     strcpy(buffer,"Warning");
1290     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1291     ok(index == 2, "Index was not incremented\n");
1292     ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
1293     len = sizeof(buffer);
1294     strcpy(buffer,"Warning");
1295     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1296
1297     
1298     ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1299 done:
1300     ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1301     ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1302 }
1303
1304 static const char contmsg[] =
1305 "HTTP/1.1 100 Continue\r\n";
1306
1307 static const char okmsg[] =
1308 "HTTP/1.1 200 OK\r\n"
1309 "Server: winetest\r\n"
1310 "\r\n";
1311
1312 static const char notokmsg[] =
1313 "HTTP/1.1 400 Bad Request\r\n"
1314 "Server: winetest\r\n"
1315 "\r\n";
1316
1317 static const char noauthmsg[] =
1318 "HTTP/1.1 401 Unauthorized\r\n"
1319 "Server: winetest\r\n"
1320 "\r\n";
1321
1322 static const char proxymsg[] =
1323 "HTTP/1.1 407 Proxy Authentication Required\r\n"
1324 "Server: winetest\r\n"
1325 "Proxy-Connection: close\r\n"
1326 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
1327 "\r\n";
1328
1329 static const char page1[] =
1330 "<HTML>\r\n"
1331 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
1332 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
1333 "</HTML>\r\n\r\n";
1334
1335 struct server_info {
1336     HANDLE hEvent;
1337     int port;
1338 };
1339
1340 static DWORD CALLBACK server_thread(LPVOID param)
1341 {
1342     struct server_info *si = param;
1343     int r, c, i, on;
1344     SOCKET s;
1345     struct sockaddr_in sa;
1346     char buffer[0x100];
1347     WSADATA wsaData;
1348     int last_request = 0;
1349
1350     WSAStartup(MAKEWORD(1,1), &wsaData);
1351
1352     s = socket(AF_INET, SOCK_STREAM, 0);
1353     if (s == INVALID_SOCKET)
1354         return 1;
1355
1356     on = 1;
1357     setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
1358
1359     memset(&sa, 0, sizeof sa);
1360     sa.sin_family = AF_INET;
1361     sa.sin_port = htons(si->port);
1362     sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
1363
1364     r = bind(s, (struct sockaddr*) &sa, sizeof sa);
1365     if (r<0)
1366         return 1;
1367
1368     listen(s, 0);
1369
1370     SetEvent(si->hEvent);
1371
1372     do
1373     {
1374         c = accept(s, NULL, NULL);
1375
1376         memset(buffer, 0, sizeof buffer);
1377         for(i=0; i<(sizeof buffer-1); i++)
1378         {
1379             r = recv(c, &buffer[i], 1, 0);
1380             if (r != 1)
1381                 break;
1382             if (i<4) continue;
1383             if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
1384                 buffer[i-3] == '\r' && buffer[i-1] == '\r')
1385                 break;
1386         }
1387         if (strstr(buffer, "GET /test1"))
1388         {
1389             if (!strstr(buffer, "Content-Length: 0"))
1390             {
1391                 send(c, okmsg, sizeof okmsg-1, 0);
1392                 send(c, page1, sizeof page1-1, 0);
1393             }
1394             else
1395                 send(c, notokmsg, sizeof notokmsg-1, 0);
1396         }
1397         if (strstr(buffer, "/test2"))
1398         {
1399             if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
1400             {
1401                 send(c, okmsg, sizeof okmsg-1, 0);
1402                 send(c, page1, sizeof page1-1, 0);
1403             }
1404             else
1405                 send(c, proxymsg, sizeof proxymsg-1, 0);
1406         }
1407         if (strstr(buffer, "/test3"))
1408         {
1409             if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
1410                 send(c, okmsg, sizeof okmsg-1, 0);
1411             else
1412                 send(c, noauthmsg, sizeof noauthmsg-1, 0);
1413         }
1414         if (strstr(buffer, "/test4"))
1415         {
1416             if (strstr(buffer, "Connection: Close"))
1417                 send(c, okmsg, sizeof okmsg-1, 0);
1418             else
1419                 send(c, notokmsg, sizeof notokmsg-1, 0);
1420         }
1421         if (strstr(buffer, "POST /test5"))
1422         {
1423             if (strstr(buffer, "Content-Length: 0"))
1424             {
1425                 send(c, okmsg, sizeof okmsg-1, 0);
1426                 send(c, page1, sizeof page1-1, 0);
1427             }
1428             else
1429                 send(c, notokmsg, sizeof notokmsg-1, 0);
1430         }
1431         if (strstr(buffer, "GET /test6"))
1432         {
1433             send(c, contmsg, sizeof contmsg-1, 0);
1434             send(c, contmsg, sizeof contmsg-1, 0);
1435             send(c, okmsg, sizeof okmsg-1, 0);
1436             send(c, page1, sizeof page1-1, 0);
1437         }
1438         if (strstr(buffer, "POST /test7"))
1439         {
1440             if (strstr(buffer, "Content-Length: 100"))
1441             {
1442                 send(c, okmsg, sizeof okmsg-1, 0);
1443                 send(c, page1, sizeof page1-1, 0);
1444             }
1445             else
1446                 send(c, notokmsg, sizeof notokmsg-1, 0);
1447         }
1448         if (strstr(buffer, "/test8"))
1449         {
1450             if (!strstr(buffer, "Connection: Close") &&
1451                  strstr(buffer, "Connection: Keep-Alive"))
1452                 send(c, okmsg, sizeof okmsg-1, 0);
1453             else
1454                 send(c, notokmsg, sizeof notokmsg-1, 0);
1455         }
1456         if (strstr(buffer, "/test9"))
1457         {
1458             if (!strstr(buffer, "Connection: Close") &&
1459                 !strstr(buffer, "Connection: Keep-Alive"))
1460                 send(c, okmsg, sizeof okmsg-1, 0);
1461             else
1462                 send(c, notokmsg, sizeof notokmsg-1, 0);
1463         }
1464         if (strstr(buffer, "GET /quit"))
1465         {
1466             send(c, okmsg, sizeof okmsg-1, 0);
1467             send(c, page1, sizeof page1-1, 0);
1468             last_request = 1;
1469         }
1470
1471         shutdown(c, 2);
1472         closesocket(c);
1473     } while (!last_request);
1474
1475     closesocket(s);
1476
1477     return 0;
1478 }
1479
1480 static void test_basic_request(int port, const char *verb, const char *url)
1481 {
1482     HINTERNET hi, hc, hr;
1483     DWORD r, count;
1484     char buffer[0x100];
1485
1486     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1487     ok(hi != NULL, "open failed\n");
1488
1489     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1490     ok(hc != NULL, "connect failed\n");
1491
1492     hr = HttpOpenRequest(hc, verb, url, NULL, NULL, NULL, 0, 0);
1493     ok(hr != NULL, "HttpOpenRequest failed\n");
1494
1495     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1496     ok(r, "HttpSendRequest failed\n");
1497
1498     count = 0;
1499     memset(buffer, 0, sizeof buffer);
1500     r = InternetReadFile(hr, buffer, sizeof buffer, &count);
1501     ok(r, "InternetReadFile failed\n");
1502     ok(count == sizeof page1 - 1, "count was wrong\n");
1503     ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
1504
1505     InternetCloseHandle(hr);
1506     InternetCloseHandle(hc);
1507     InternetCloseHandle(hi);
1508 }
1509
1510 static void test_proxy_indirect(int port)
1511 {
1512     HINTERNET hi, hc, hr;
1513     DWORD r, sz, val;
1514     char buffer[0x40];
1515
1516     hi = InternetOpen(NULL, 0, NULL, NULL, 0);
1517     ok(hi != NULL, "open failed\n");
1518
1519     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1520     ok(hc != NULL, "connect failed\n");
1521
1522     hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1523     ok(hr != NULL, "HttpOpenRequest failed\n");
1524
1525     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1526     ok(r, "HttpSendRequest failed\n");
1527
1528     sz = sizeof buffer;
1529     r = HttpQueryInfo(hr, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
1530     ok(r, "HttpQueryInfo failed\n");
1531     ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
1532
1533     sz = sizeof buffer;
1534     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1535     ok(r, "HttpQueryInfo failed\n");
1536     ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1537
1538     sz = sizeof val;
1539     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &val, &sz, NULL);
1540     ok(r, "HttpQueryInfo failed\n");
1541     ok(val == 407, "proxy code wrong\n");
1542
1543     sz = sizeof buffer;
1544     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
1545     ok(r, "HttpQueryInfo failed\n");
1546     ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
1547
1548     sz = sizeof buffer;
1549     r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &sz, NULL);
1550     ok(r, "HttpQueryInfo failed\n");
1551     ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
1552
1553     sz = sizeof buffer;
1554     r = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &sz, NULL);
1555     ok(r, "HttpQueryInfo failed\n");
1556     ok(!strcmp(buffer, "winetest"), "http server wrong\n");
1557
1558     sz = sizeof buffer;
1559     r = HttpQueryInfo(hr, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
1560     ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
1561     ok(r == FALSE, "HttpQueryInfo failed\n");
1562
1563     InternetCloseHandle(hr);
1564     InternetCloseHandle(hc);
1565     InternetCloseHandle(hi);
1566 }
1567
1568 static void test_proxy_direct(int port)
1569 {
1570     HINTERNET hi, hc, hr;
1571     DWORD r, sz;
1572     char buffer[0x40];
1573     static CHAR username[] = "mike",
1574                 password[] = "1101";
1575
1576     sprintf(buffer, "localhost:%d\n", port);
1577     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
1578     ok(hi != NULL, "open failed\n");
1579
1580     /* try connect without authorization */
1581     hc = InternetConnect(hi, "www.winehq.org/", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1582     ok(hc != NULL, "connect failed\n");
1583
1584     hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1585     ok(hr != NULL, "HttpOpenRequest failed\n");
1586
1587     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1588     ok(r, "HttpSendRequest failed\n");
1589
1590     sz = sizeof buffer;
1591     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1592     ok(r, "HttpQueryInfo failed\n");
1593     ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1594
1595
1596     /* set the user + password then try again */
1597     todo_wine {
1598     r = InternetSetOption(hr, INTERNET_OPTION_PROXY_USERNAME, username, 4);
1599     ok(r, "failed to set user\n");
1600
1601     r = InternetSetOption(hr, INTERNET_OPTION_PROXY_PASSWORD, password, 4);
1602     ok(r, "failed to set password\n");
1603     }
1604
1605     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1606     ok(r, "HttpSendRequest failed\n");
1607     sz = sizeof buffer;
1608     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1609     ok(r, "HttpQueryInfo failed\n");
1610     todo_wine {
1611     ok(!strcmp(buffer, "200"), "proxy code wrong\n");
1612     }
1613
1614
1615     InternetCloseHandle(hr);
1616     InternetCloseHandle(hc);
1617     InternetCloseHandle(hi);
1618 }
1619
1620 static void test_header_handling_order(int port)
1621 {
1622     static char authorization[] = "Authorization: Basic dXNlcjpwd2Q=";
1623     static char connection[]    = "Connection: Close";
1624
1625     static const char *types[2] = { "*", NULL };
1626     HINTERNET session, connect, request;
1627     DWORD size, status;
1628     BOOL ret;
1629
1630     session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1631     ok(session != NULL, "InternetOpen failed\n");
1632
1633     connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1634     ok(connect != NULL, "InternetConnect failed\n");
1635
1636     request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1637     ok(request != NULL, "HttpOpenRequest failed\n");
1638
1639     ret = HttpAddRequestHeaders(request, authorization, ~0UL, HTTP_ADDREQ_FLAG_ADD);
1640     ok(ret, "HttpAddRequestHeaders failed\n");
1641
1642     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1643     ok(ret, "HttpSendRequest failed\n");
1644
1645     status = 0;
1646     size = sizeof(status);
1647     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1648     ok(ret, "HttpQueryInfo failed\n");
1649     ok(status == 200, "request failed with status %u\n", status);
1650
1651     InternetCloseHandle(request);
1652
1653     request = HttpOpenRequest(connect, NULL, "/test4", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1654     ok(request != NULL, "HttpOpenRequest failed\n");
1655
1656     ret = HttpSendRequest(request, connection, ~0UL, NULL, 0);
1657     ok(ret, "HttpSendRequest failed\n");
1658
1659     status = 0;
1660     size = sizeof(status);
1661     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1662     ok(ret, "HttpQueryInfo failed\n");
1663     ok(status == 200, "request failed with status %u\n", status);
1664
1665     InternetCloseHandle(request);
1666
1667     request = HttpOpenRequest(connect, "POST", "/test7", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1668     ok(request != NULL, "HttpOpenRequest failed\n");
1669
1670     ret = HttpAddRequestHeaders(request, "Content-Length: 100\r\n", ~0UL, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
1671     ok(ret, "HttpAddRequestHeaders failed\n");
1672
1673     ret = HttpSendRequest(request, connection, ~0UL, NULL, 0);
1674     ok(ret, "HttpSendRequest failed\n");
1675
1676     status = 0;
1677     size = sizeof(status);
1678     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1679     ok(ret, "HttpQueryInfo failed\n");
1680     ok(status == 200 || status == 400 /* IE6 */, "request failed with status %u\n", status);
1681
1682     InternetCloseHandle(request);
1683     InternetCloseHandle(connect);
1684     InternetCloseHandle(session);
1685 }
1686
1687 static void test_connection_header(int port)
1688 {
1689     HINTERNET ses, con, req;
1690     DWORD size, status;
1691     BOOL ret;
1692
1693     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1694     ok(ses != NULL, "InternetOpen failed\n");
1695
1696     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1697     ok(con != NULL, "InternetConnect failed\n");
1698
1699     req = HttpOpenRequest(con, NULL, "/test8", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1700     ok(req != NULL, "HttpOpenRequest failed\n");
1701
1702     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1703     ok(ret, "HttpSendRequest failed\n");
1704
1705     status = 0;
1706     size = sizeof(status);
1707     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1708     ok(ret, "HttpQueryInfo failed\n");
1709     ok(status == 200, "request failed with status %u\n", status);
1710
1711     InternetCloseHandle(req);
1712
1713     req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, 0, 0);
1714     ok(req != NULL, "HttpOpenRequest failed\n");
1715
1716     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1717     ok(ret, "HttpSendRequest failed\n");
1718
1719     status = 0;
1720     size = sizeof(status);
1721     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1722     ok(ret, "HttpQueryInfo failed\n");
1723     ok(status == 200, "request failed with status %u\n", status);
1724
1725     InternetCloseHandle(req);
1726     InternetCloseHandle(con);
1727     InternetCloseHandle(ses);
1728 }
1729
1730 static void test_http_connection(void)
1731 {
1732     struct server_info si;
1733     HANDLE hThread;
1734     DWORD id = 0, r;
1735
1736     si.hEvent = CreateEvent(NULL, 0, 0, NULL);
1737     si.port = 7531;
1738
1739     hThread = CreateThread(NULL, 0, server_thread, (LPVOID) &si, 0, &id);
1740     ok( hThread != NULL, "create thread failed\n");
1741
1742     r = WaitForSingleObject(si.hEvent, 10000);
1743     ok (r == WAIT_OBJECT_0, "failed to start wininet test server\n");
1744     if (r != WAIT_OBJECT_0)
1745         return;
1746
1747     test_basic_request(si.port, "GET", "/test1");
1748     test_proxy_indirect(si.port);
1749     test_proxy_direct(si.port);
1750     test_header_handling_order(si.port);
1751     test_basic_request(si.port, "POST", "/test5");
1752     test_basic_request(si.port, "GET", "/test6");
1753     test_connection_header(si.port);
1754
1755     /* send the basic request again to shutdown the server thread */
1756     test_basic_request(si.port, "GET", "/quit");
1757
1758     r = WaitForSingleObject(hThread, 3000);
1759     ok( r == WAIT_OBJECT_0, "thread wait failed\n");
1760     CloseHandle(hThread);
1761 }
1762
1763 static void test_user_agent_header(void)
1764 {
1765     HINTERNET ses, con, req;
1766     DWORD size, err;
1767     char buffer[64];
1768     BOOL ret;
1769
1770     ses = InternetOpen("Gizmo5", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1771     ok(ses != NULL, "InternetOpen failed\n");
1772
1773     con = InternetConnect(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1774     ok(con != NULL, "InternetConnect failed\n");
1775
1776     req = HttpOpenRequest(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
1777     ok(req != NULL, "HttpOpenRequest failed\n");
1778
1779     size = sizeof(buffer);
1780     ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1781     err = GetLastError();
1782     ok(!ret, "HttpQueryInfo succeeded\n");
1783     ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
1784
1785     ret = HttpAddRequestHeaders(req, "User-Agent: Gizmo Project\r\n", ~0UL, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
1786     ok(ret, "HttpAddRequestHeaders succeeded\n");
1787
1788     size = sizeof(buffer);
1789     ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1790     err = GetLastError();
1791     ok(ret, "HttpQueryInfo failed\n");
1792     ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
1793
1794     InternetCloseHandle(req);
1795
1796     req = HttpOpenRequest(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
1797     ok(req != NULL, "HttpOpenRequest failed\n");
1798
1799     size = sizeof(buffer);
1800     ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1801     err = GetLastError();
1802     ok(!ret, "HttpQueryInfo succeeded\n");
1803     ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
1804
1805     ret = HttpAddRequestHeaders(req, "Accept: audio/*, image/*, text/*\r\nUser-Agent: Gizmo Project\r\n", ~0UL, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
1806     ok(ret, "HttpAddRequestHeaders failed\n");
1807
1808     buffer[0] = 0;
1809     size = sizeof(buffer);
1810     ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1811     ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
1812     ok(!strcmp(buffer, "audio/*, image/*, text/*"), "got '%s' expected 'audio/*, image/*, text/*'\n", buffer);
1813
1814     InternetCloseHandle(req);
1815     InternetCloseHandle(con);
1816     InternetCloseHandle(ses);
1817 }
1818
1819 static void test_bogus_accept_types_array(void)
1820 {
1821     HINTERNET ses, con, req;
1822     static const char *types[] = { (const char *)6240, "*/*", "%p", "", "*/*", NULL };
1823     DWORD size;
1824     char buffer[32];
1825     BOOL ret;
1826
1827     ses = InternetOpen("MERONG(0.9/;p)", INTERNET_OPEN_TYPE_DIRECT, "", "", 0);
1828     con = InternetConnect(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1829     req = HttpOpenRequest(con, "POST", "/post/post_action.php", "HTTP/1.0", "", types, INTERNET_FLAG_FORMS_SUBMIT, 0);
1830
1831     ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
1832
1833     buffer[0] = 0;
1834     size = sizeof(buffer);
1835     ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1836     ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
1837     ok(!strcmp(buffer, ", */*, %p, , */*") || /* IE6 */
1838        !strcmp(buffer, "*/*, %p, */*"),
1839        "got '%s' expected '*/*, %%p, */*' or ', */*, %%p, , */*'\n", buffer);
1840
1841     InternetCloseHandle(req);
1842     InternetCloseHandle(con);
1843     InternetCloseHandle(ses);
1844 }
1845
1846 struct context
1847 {
1848     HANDLE event;
1849     HINTERNET req;
1850 };
1851
1852 static void WINAPI cb(HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID info, DWORD size)
1853 {
1854     INTERNET_ASYNC_RESULT *result = info;
1855     struct context *ctx = (struct context *)context;
1856
1857     trace("%p 0x%08lx %u %p 0x%08x\n", handle, context, status, info, size);
1858
1859     if (status == INTERNET_STATUS_REQUEST_COMPLETE)
1860     {
1861         trace("request handle: 0x%08lx\n", result->dwResult);
1862         ctx->req = (HINTERNET)result->dwResult;
1863         SetEvent(ctx->event);
1864     }
1865     if (status == INTERNET_STATUS_HANDLE_CLOSING)
1866     {
1867         DWORD type = INTERNET_HANDLE_TYPE_CONNECT_HTTP, size = sizeof(type);
1868
1869         if (InternetQueryOption(handle, INTERNET_OPTION_HANDLE_TYPE, &type, &size))
1870             ok(type != INTERNET_HANDLE_TYPE_CONNECT_HTTP, "unexpected callback\n");
1871         SetEvent(ctx->event);
1872     }
1873 }
1874
1875 static void test_open_url_async(void)
1876 {
1877     BOOL ret;
1878     HINTERNET ses, req;
1879     DWORD size;
1880     struct context ctx;
1881     ULONG type;
1882
1883     ctx.req = NULL;
1884     ctx.event = CreateEvent(NULL, TRUE, FALSE, "Z:_home_hans_jaman-installer.exe_ev1");
1885
1886     ses = InternetOpen("AdvancedInstaller", 0, NULL, NULL, INTERNET_FLAG_ASYNC);
1887     ok(ses != NULL, "InternetOpen failed\n");
1888
1889     pInternetSetStatusCallbackA(ses, cb);
1890     ResetEvent(ctx.event);
1891
1892     req = InternetOpenUrl(ses, "http://www.winehq.org", NULL, 0, 0, (DWORD_PTR)&ctx);
1893     ok(!req && GetLastError() == ERROR_IO_PENDING, "InternetOpenUrl failed\n");
1894
1895     WaitForSingleObject(ctx.event, INFINITE);
1896
1897     type = 0;
1898     size = sizeof(type);
1899     ret = InternetQueryOption(ctx.req, INTERNET_OPTION_HANDLE_TYPE, &type, &size);
1900     ok(ret, "InternetQueryOption failed: %u\n", GetLastError());
1901     ok(type == INTERNET_HANDLE_TYPE_HTTP_REQUEST,
1902        "expected INTERNET_HANDLE_TYPE_HTTP_REQUEST, got %u\n", type);
1903
1904     size = 0;
1905     ret = HttpQueryInfo(ctx.req, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &size, NULL);
1906     ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "HttpQueryInfo failed\n");
1907     ok(size > 0, "expected size > 0\n");
1908
1909     ResetEvent(ctx.event);
1910     InternetCloseHandle(ctx.req);
1911     WaitForSingleObject(ctx.event, INFINITE);
1912
1913     InternetCloseHandle(ses);
1914     CloseHandle(ctx.event);
1915 }
1916
1917 #define STATUS_STRING(status) \
1918     memcpy(status_string[status], #status, sizeof(CHAR) * \
1919            (strlen(#status) < MAX_STATUS_NAME ? \
1920             strlen(#status) : \
1921             MAX_STATUS_NAME - 1))
1922 static void init_status_tests(void)
1923 {
1924     memset(expect, 0, sizeof(expect));
1925     memset(wine_allow, 0, sizeof(wine_allow));
1926     memset(notified, 0, sizeof(notified));
1927     memset(status_string, 0, sizeof(status_string));
1928     STATUS_STRING(INTERNET_STATUS_RESOLVING_NAME);
1929     STATUS_STRING(INTERNET_STATUS_NAME_RESOLVED);
1930     STATUS_STRING(INTERNET_STATUS_CONNECTING_TO_SERVER);
1931     STATUS_STRING(INTERNET_STATUS_CONNECTED_TO_SERVER);
1932     STATUS_STRING(INTERNET_STATUS_SENDING_REQUEST);
1933     STATUS_STRING(INTERNET_STATUS_REQUEST_SENT);
1934     STATUS_STRING(INTERNET_STATUS_RECEIVING_RESPONSE);
1935     STATUS_STRING(INTERNET_STATUS_RESPONSE_RECEIVED);
1936     STATUS_STRING(INTERNET_STATUS_CTL_RESPONSE_RECEIVED);
1937     STATUS_STRING(INTERNET_STATUS_PREFETCH);
1938     STATUS_STRING(INTERNET_STATUS_CLOSING_CONNECTION);
1939     STATUS_STRING(INTERNET_STATUS_CONNECTION_CLOSED);
1940     STATUS_STRING(INTERNET_STATUS_HANDLE_CREATED);
1941     STATUS_STRING(INTERNET_STATUS_HANDLE_CLOSING);
1942     STATUS_STRING(INTERNET_STATUS_DETECTING_PROXY);
1943     STATUS_STRING(INTERNET_STATUS_REQUEST_COMPLETE);
1944     STATUS_STRING(INTERNET_STATUS_REDIRECT);
1945     STATUS_STRING(INTERNET_STATUS_INTERMEDIATE_RESPONSE);
1946     STATUS_STRING(INTERNET_STATUS_USER_INPUT_REQUIRED);
1947     STATUS_STRING(INTERNET_STATUS_STATE_CHANGE);
1948     STATUS_STRING(INTERNET_STATUS_COOKIE_SENT);
1949     STATUS_STRING(INTERNET_STATUS_COOKIE_RECEIVED);
1950     STATUS_STRING(INTERNET_STATUS_PRIVACY_IMPACTED);
1951     STATUS_STRING(INTERNET_STATUS_P3P_HEADER);
1952     STATUS_STRING(INTERNET_STATUS_P3P_POLICYREF);
1953     STATUS_STRING(INTERNET_STATUS_COOKIE_HISTORY);
1954 }
1955 #undef STATUS_STRING
1956
1957 START_TEST(http)
1958 {
1959     HMODULE hdll;
1960     hdll = GetModuleHandleA("wininet.dll");
1961     pInternetSetStatusCallbackA = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackA");
1962     pInternetTimeFromSystemTimeA = (void*)GetProcAddress(hdll, "InternetTimeFromSystemTimeA");
1963     pInternetTimeFromSystemTimeW = (void*)GetProcAddress(hdll, "InternetTimeFromSystemTimeW");
1964     pInternetTimeToSystemTimeA = (void*)GetProcAddress(hdll, "InternetTimeToSystemTimeA");
1965     pInternetTimeToSystemTimeW = (void*)GetProcAddress(hdll, "InternetTimeToSystemTimeW");
1966
1967     if (!pInternetSetStatusCallbackA)
1968         skip("skipping the InternetReadFile tests\n");
1969     else
1970     {
1971         init_status_tests();
1972         InternetReadFile_test(INTERNET_FLAG_ASYNC);
1973         InternetReadFile_test(0);
1974         InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
1975         test_open_url_async();
1976     }
1977     InternetOpenRequest_test();
1978     test_http_cache();
1979     InternetOpenUrlA_test();
1980     if (!pInternetTimeFromSystemTimeA)
1981         skip("skipping the InternetTime tests\n");
1982     else
1983     {
1984         InternetTimeFromSystemTimeA_test();
1985         InternetTimeFromSystemTimeW_test();
1986         InternetTimeToSystemTimeA_test();
1987         InternetTimeToSystemTimeW_test();
1988     }
1989     HttpSendRequestEx_test();
1990     HttpHeaders_test();
1991     test_http_connection();
1992     test_user_agent_header();
1993     test_bogus_accept_types_array();
1994 }