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