setupapi/tests: Use GetModuleHandleA instead of LoadLibraryA.
[wine] / dlls / wininet / tests / http.c
1 /*
2  * Wininet - Http tests
3  *
4  * Copyright 2002 Aric Stewart
5  * Copyright 2004 Mike McCormack
6  * Copyright 2005 Hans Leidekker
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wininet.h"
30 #include "winsock.h"
31
32 #include "wine/test.h"
33
34 #define TEST_URL "http://www.winehq.org/site/about"
35
36 static BOOL first_connection_to_test_url = TRUE;
37
38 /* Adapted from dlls/urlmon/tests/protocol.c */
39
40 #define SET_EXPECT2(status, num) \
41     expect[status] = num
42
43 #define SET_EXPECT(status) \
44     SET_EXPECT2(status, 1)
45
46 /* SET_WINE_ALLOW's should be used with an appropriate
47  * todo_wine CHECK_NOTIFIED at a later point in the code */
48 #define SET_WINE_ALLOW2(status, num) \
49     wine_allow[status] = num
50
51 #define SET_WINE_ALLOW(status) \
52     SET_WINE_ALLOW2(status, 1)
53
54 #define CHECK_EXPECT(status) \
55     do { \
56         if (!expect[status] && wine_allow[status]) \
57         { \
58             todo_wine ok(expect[status], "unexpected status %d (%s)\n", status, \
59                          status < MAX_INTERNET_STATUS && status_string[status][0] != 0 ? \
60                          status_string[status] : "unknown");            \
61             wine_allow[status]--; \
62         } \
63         else \
64         { \
65             ok(expect[status], "unexpected status %d (%s)\n", status,   \
66                status < MAX_INTERNET_STATUS && status_string[status][0] != 0 ? \
67                status_string[status] : "unknown");                      \
68             expect[status]--;                                           \
69         } \
70         notified[status]++; \
71     }while(0)
72
73 /* CLEAR_NOTIFIED used in cases when notification behavior
74  * differs between Windows versions */
75 #define CLEAR_NOTIFIED(status) \
76     expect[status] = wine_allow[status] = notified[status] = 0;
77
78 #define CHECK_NOTIFIED2(status, num) \
79     do { \
80         ok(notified[status] == (num), "expected status %d (%s) %d times, received %d times\n", \
81            status, status < MAX_INTERNET_STATUS && status_string[status][0] != 0 ? \
82            status_string[status] : "unknown", (num), notified[status]); \
83         CLEAR_NOTIFIED(status);                                         \
84     }while(0)
85
86 #define CHECK_NOTIFIED(status) \
87     CHECK_NOTIFIED2(status, 1)
88
89 #define CHECK_NOT_NOTIFIED(status) \
90     CHECK_NOTIFIED2(status, 0)
91
92 #define MAX_INTERNET_STATUS (INTERNET_STATUS_COOKIE_HISTORY+1)
93 #define MAX_STATUS_NAME 50
94 static int expect[MAX_INTERNET_STATUS], wine_allow[MAX_INTERNET_STATUS],
95     notified[MAX_INTERNET_STATUS];
96 static CHAR status_string[MAX_INTERNET_STATUS][MAX_STATUS_NAME];
97
98 static HANDLE hCompleteEvent;
99
100 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET ,INTERNET_STATUS_CALLBACK);
101 static BOOL (WINAPI *pInternetTimeFromSystemTimeA)(CONST SYSTEMTIME *,DWORD ,LPSTR ,DWORD);
102 static BOOL (WINAPI *pInternetTimeFromSystemTimeW)(CONST SYSTEMTIME *,DWORD ,LPWSTR ,DWORD);
103 static BOOL (WINAPI *pInternetTimeToSystemTimeA)(LPCSTR ,SYSTEMTIME *,DWORD);
104 static BOOL (WINAPI *pInternetTimeToSystemTimeW)(LPCWSTR ,SYSTEMTIME *,DWORD);
105
106
107 static VOID WINAPI callback(
108      HINTERNET hInternet,
109      DWORD_PTR dwContext,
110      DWORD dwInternetStatus,
111      LPVOID lpvStatusInformation,
112      DWORD dwStatusInformationLength
113 )
114 {
115     CHECK_EXPECT(dwInternetStatus);
116     switch (dwInternetStatus)
117     {
118         case INTERNET_STATUS_RESOLVING_NAME:
119             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESOLVING_NAME \"%s\" %d\n",
120                 GetCurrentThreadId(), hInternet, dwContext,
121                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
122             *(LPSTR)lpvStatusInformation = '\0';
123             break;
124         case INTERNET_STATUS_NAME_RESOLVED:
125             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_NAME_RESOLVED \"%s\" %d\n",
126                 GetCurrentThreadId(), hInternet, dwContext,
127                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
128             *(LPSTR)lpvStatusInformation = '\0';
129             break;
130         case INTERNET_STATUS_CONNECTING_TO_SERVER:
131             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %d\n",
132                 GetCurrentThreadId(), hInternet, dwContext,
133                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
134             *(LPSTR)lpvStatusInformation = '\0';
135             break;
136         case INTERNET_STATUS_CONNECTED_TO_SERVER:
137             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %d\n",
138                 GetCurrentThreadId(), hInternet, dwContext,
139                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
140             *(LPSTR)lpvStatusInformation = '\0';
141             break;
142         case INTERNET_STATUS_SENDING_REQUEST:
143             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_SENDING_REQUEST %p %d\n",
144                 GetCurrentThreadId(), hInternet, dwContext,
145                 lpvStatusInformation,dwStatusInformationLength);
146             break;
147         case INTERNET_STATUS_REQUEST_SENT:
148             ok(dwStatusInformationLength == sizeof(DWORD),
149                 "info length should be sizeof(DWORD) instead of %d\n",
150                 dwStatusInformationLength);
151             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_SENT 0x%x %d\n",
152                 GetCurrentThreadId(), hInternet, dwContext,
153                 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
154             break;
155         case INTERNET_STATUS_RECEIVING_RESPONSE:
156             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RECEIVING_RESPONSE %p %d\n",
157                 GetCurrentThreadId(), hInternet, dwContext,
158                 lpvStatusInformation,dwStatusInformationLength);
159             break;
160         case INTERNET_STATUS_RESPONSE_RECEIVED:
161             ok(dwStatusInformationLength == sizeof(DWORD),
162                 "info length should be sizeof(DWORD) instead of %d\n",
163                 dwStatusInformationLength);
164             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESPONSE_RECEIVED 0x%x %d\n",
165                 GetCurrentThreadId(), hInternet, dwContext,
166                 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
167             break;
168         case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
169             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %d\n",
170                 GetCurrentThreadId(), hInternet,dwContext,
171                 lpvStatusInformation,dwStatusInformationLength);
172             break;
173         case INTERNET_STATUS_PREFETCH:
174             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_PREFETCH %p %d\n",
175                 GetCurrentThreadId(), hInternet, dwContext,
176                 lpvStatusInformation,dwStatusInformationLength);
177             break;
178         case INTERNET_STATUS_CLOSING_CONNECTION:
179             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CLOSING_CONNECTION %p %d\n",
180                 GetCurrentThreadId(), hInternet, dwContext,
181                 lpvStatusInformation,dwStatusInformationLength);
182             break;
183         case INTERNET_STATUS_CONNECTION_CLOSED:
184             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTION_CLOSED %p %d\n",
185                 GetCurrentThreadId(), hInternet, dwContext,
186                 lpvStatusInformation,dwStatusInformationLength);
187             break;
188         case INTERNET_STATUS_HANDLE_CREATED:
189             ok(dwStatusInformationLength == sizeof(HINTERNET),
190                 "info length should be sizeof(HINTERNET) instead of %d\n",
191                 dwStatusInformationLength);
192             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CREATED %p %d\n",
193                 GetCurrentThreadId(), hInternet, dwContext,
194                 *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
195             break;
196         case INTERNET_STATUS_HANDLE_CLOSING:
197             ok(dwStatusInformationLength == sizeof(HINTERNET),
198                 "info length should be sizeof(HINTERNET) instead of %d\n",
199                 dwStatusInformationLength);
200             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
201                 GetCurrentThreadId(), hInternet, dwContext,
202                 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
203             break;
204         case INTERNET_STATUS_REQUEST_COMPLETE:
205         {
206             INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
207             ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
208                 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
209                 dwStatusInformationLength);
210             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%d,%d} %d\n",
211                 GetCurrentThreadId(), hInternet, dwContext,
212                 iar->dwResult,iar->dwError,dwStatusInformationLength);
213             SetEvent(hCompleteEvent);
214             break;
215         }
216         case INTERNET_STATUS_REDIRECT:
217             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
218                 GetCurrentThreadId(), hInternet, dwContext,
219                 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
220             *(LPSTR)lpvStatusInformation = '\0';
221             break;
222         case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
223             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
224                 GetCurrentThreadId(), hInternet, dwContext,
225                 lpvStatusInformation, dwStatusInformationLength);
226             break;
227         default:
228             trace("%04x:Callback %p 0x%lx %d %p %d\n",
229                 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
230                 lpvStatusInformation, dwStatusInformationLength);
231     }
232 }
233
234 static void InternetReadFile_test(int flags)
235 {
236     DWORD rc;
237     CHAR buffer[4000];
238     DWORD length;
239     DWORD out;
240     const char *types[2] = { "*", NULL };
241     HINTERNET hi, hic = 0, hor = 0;
242
243     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
244
245     trace("Starting InternetReadFile test with flags 0x%x\n",flags);
246
247     trace("InternetOpenA <--\n");
248     hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
249     ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
250     trace("InternetOpenA -->\n");
251
252     if (hi == 0x0) goto abort;
253
254     pInternetSetStatusCallbackA(hi,&callback);
255
256     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
257
258     trace("InternetConnectA <--\n");
259     hic=InternetConnectA(hi, "www.winehq.org", INTERNET_INVALID_PORT_NUMBER,
260                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
261     ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
262     trace("InternetConnectA -->\n");
263
264     if (hic == 0x0) goto abort;
265
266     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
267     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
268     SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
269     SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
270
271     trace("HttpOpenRequestA <--\n");
272     hor = HttpOpenRequestA(hic, "GET", "/about/", NULL, NULL, types,
273                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
274                            0xdeadbead);
275     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
276         /*
277          * If the internet name can't be resolved we are probably behind
278          * a firewall or in some other way not directly connected to the
279          * Internet. Not enough reason to fail the test. Just ignore and
280          * abort.
281          */
282     } else  {
283         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
284     }
285     trace("HttpOpenRequestA -->\n");
286
287     if (hor == 0x0) goto abort;
288
289     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
290     todo_wine
291     {
292         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
293         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
294     }
295     if (first_connection_to_test_url)
296     {
297         SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
298         SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
299     }
300     else
301     {
302         SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
303         SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
304     }
305     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
306     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
307     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
308     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
309     SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
310     SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
311     SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
312     SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
313     SET_EXPECT(INTERNET_STATUS_REDIRECT);
314     if (flags & INTERNET_FLAG_ASYNC)
315         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
316     else
317         SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
318
319     trace("HttpSendRequestA -->\n");
320     SetLastError(0xdeadbeef);
321     rc = HttpSendRequestA(hor, "", -1, NULL, 0);
322     if (flags & INTERNET_FLAG_ASYNC)
323         ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
324             "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
325     else
326         ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
327            "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
328     trace("HttpSendRequestA <--\n");
329
330     if (flags & INTERNET_FLAG_ASYNC)
331         WaitForSingleObject(hCompleteEvent, INFINITE);
332
333     if (first_connection_to_test_url)
334     {
335         CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
336         CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
337     }
338     else todo_wine
339     {
340         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
341         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
342     }
343     CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
344     CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
345     CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
346     CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
347     CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
348     if (flags & INTERNET_FLAG_ASYNC)
349         CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
350     else
351         todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
352     /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
353     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
354     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
355
356     length = 4;
357     rc = InternetQueryOptionA(hor,INTERNET_OPTION_REQUEST_FLAGS,&out,&length);
358     trace("Option 0x17 -> %i  %i\n",rc,out);
359
360     length = 100;
361     rc = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
362     trace("Option 0x22 -> %i  %s\n",rc,buffer);
363
364     length = 4000;
365     rc = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
366     buffer[length]=0;
367     trace("Option 0x16 -> %i  %s\n",rc,buffer);
368
369     length = 4000;
370     rc = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
371     buffer[length]=0;
372     trace("Option 0x22 -> %i  %s\n",rc,buffer);
373
374     length = 16;
375     rc = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
376     trace("Option 0x5 -> %i  %s  (%u)\n",rc,buffer,GetLastError());
377
378     length = 100;
379     rc = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
380     buffer[length]=0;
381     trace("Option 0x1 -> %i  %s\n",rc,buffer);
382
383     SetLastError(0xdeadbeef);
384     rc = InternetReadFile(NULL, buffer, 100, &length);
385     ok(!rc, "InternetReadFile should have failed\n");
386     ok(GetLastError() == ERROR_INVALID_HANDLE,
387         "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
388         GetLastError());
389
390     length = 100;
391     trace("Entering Query loop\n");
392
393     SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
394     SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
395     while (TRUE)
396     {
397         if (flags & INTERNET_FLAG_ASYNC)
398             SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
399         rc = InternetQueryDataAvailable(hor,&length,0x0,0x0);
400         ok(!(rc == 0 && length != 0),"InternetQueryDataAvailable failed\n");
401         if (flags & INTERNET_FLAG_ASYNC)
402         {
403             if (rc != 0)
404             {
405                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
406             }
407             else if (GetLastError() == ERROR_IO_PENDING)
408             {
409                 WaitForSingleObject(hCompleteEvent, INFINITE);
410                 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
411                 continue;
412             }
413         }
414         if (length)
415         {
416             char *buffer;
417             buffer = HeapAlloc(GetProcessHeap(),0,length+1);
418
419             rc = InternetReadFile(hor,buffer,length,&length);
420
421             buffer[length]=0;
422
423             trace("ReadFile -> %i %i\n",rc,length);
424
425             HeapFree(GetProcessHeap(),0,buffer);
426         }
427         if (length == 0)
428             break;
429     }
430     /* WinXP does not send, but Win98 does */
431     CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
432     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
433 abort:
434     SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
435     if (hor != 0x0) {
436         SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
437         SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
438         SetLastError(0xdeadbeef);
439         rc = InternetCloseHandle(hor);
440         ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
441         SetLastError(0xdeadbeef);
442         rc = InternetCloseHandle(hor);
443         ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
444         ok (GetLastError() == ERROR_INVALID_HANDLE,
445             "Double close of handle should have set ERROR_INVALID_HANDLE instead of %u\n",
446             GetLastError());
447     }
448     if (hic != 0x0) {
449         rc = InternetCloseHandle(hic);
450         ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed\n");
451     }
452     if (hi != 0x0) {
453       SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
454       rc = InternetCloseHandle(hi);
455       ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
456       if (flags & INTERNET_FLAG_ASYNC)
457           Sleep(100);
458     }
459     todo_wine CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
460     if (hor != 0x0) todo_wine
461     {
462         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
463         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
464     }
465     else
466     {
467         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
468         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
469     }
470     CloseHandle(hCompleteEvent);
471     first_connection_to_test_url = FALSE;
472 }
473
474 static void InternetReadFileExA_test(int flags)
475 {
476     DWORD rc;
477     DWORD length;
478     const char *types[2] = { "*", NULL };
479     HINTERNET hi, hic = 0, hor = 0;
480     INTERNET_BUFFERS inetbuffers;
481
482     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
483
484     trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
485
486     trace("InternetOpenA <--\n");
487     hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
488     ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
489     trace("InternetOpenA -->\n");
490
491     if (hi == 0x0) goto abort;
492
493     pInternetSetStatusCallbackA(hi,&callback);
494
495     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
496
497     trace("InternetConnectA <--\n");
498     hic=InternetConnectA(hi, "www.winehq.org", INTERNET_INVALID_PORT_NUMBER,
499                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
500     ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
501     trace("InternetConnectA -->\n");
502
503     if (hic == 0x0) goto abort;
504
505     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
506     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
507     SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
508     SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
509
510     trace("HttpOpenRequestA <--\n");
511     hor = HttpOpenRequestA(hic, "GET", "/about/", NULL, NULL, types,
512                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
513                            0xdeadbead);
514     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
515         /*
516          * If the internet name can't be resolved we are probably behind
517          * a firewall or in some other way not directly connected to the
518          * Internet. Not enough reason to fail the test. Just ignore and
519          * abort.
520          */
521     } else  {
522         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
523     }
524     trace("HttpOpenRequestA -->\n");
525
526     if (hor == 0x0) goto abort;
527
528     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
529     todo_wine
530     {
531         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
532         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
533     }
534     if (first_connection_to_test_url)
535     {
536         SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
537         SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
538     }
539     else
540     {
541         SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
542         SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
543     }
544     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
545     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
546     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
547     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
548     SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
549     SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
550     SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
551     SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
552     SET_EXPECT(INTERNET_STATUS_REDIRECT);
553     if (flags & INTERNET_FLAG_ASYNC)
554         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
555     else
556         SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
557
558     trace("HttpSendRequestA -->\n");
559     SetLastError(0xdeadbeef);
560     rc = HttpSendRequestA(hor, "", -1, NULL, 0);
561     if (flags & INTERNET_FLAG_ASYNC)
562         ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
563             "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
564     else
565         ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
566            "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
567     trace("HttpSendRequestA <--\n");
568
569     if (!rc && (GetLastError() == ERROR_IO_PENDING))
570         WaitForSingleObject(hCompleteEvent, INFINITE);
571
572     if (first_connection_to_test_url)
573     {
574         CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
575         CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
576     }
577     else todo_wine
578     {
579         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
580         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
581     }
582     CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
583     CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
584     CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
585     CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
586     CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
587     if (flags & INTERNET_FLAG_ASYNC)
588         CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
589     else
590         todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
591     /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
592     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
593     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
594
595     /* tests invalid dwStructSize */
596     inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS)+1;
597     inetbuffers.lpcszHeader = NULL;
598     inetbuffers.dwHeadersLength = 0;
599     inetbuffers.dwBufferLength = 10;
600     inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
601     inetbuffers.dwOffsetHigh = 1234;
602     inetbuffers.dwOffsetLow = 5678;
603     rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
604     ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
605         "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
606         rc ? "TRUE" : "FALSE", GetLastError());
607     HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
608
609     /* tests to see whether lpcszHeader is used - it isn't */
610     inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
611     inetbuffers.lpcszHeader = (LPCTSTR)0xdeadbeef;
612     inetbuffers.dwHeadersLength = 255;
613     inetbuffers.dwBufferLength = 0;
614     inetbuffers.lpvBuffer = NULL;
615     inetbuffers.dwOffsetHigh = 1234;
616     inetbuffers.dwOffsetLow = 5678;
617     SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
618     SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
619     rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
620     ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
621     todo_wine
622     {
623         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
624         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
625     }
626
627     rc = InternetReadFileEx(NULL, &inetbuffers, 0, 0xdeadcafe);
628     ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
629         "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
630         rc ? "TRUE" : "FALSE", GetLastError());
631
632     length = 0;
633     trace("Entering Query loop\n");
634
635     SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
636     SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
637     while (TRUE)
638     {
639         inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
640         inetbuffers.dwBufferLength = 1024;
641         inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
642         inetbuffers.dwOffsetHigh = 1234;
643         inetbuffers.dwOffsetLow = 5678;
644
645         SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
646         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
647         SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
648         rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
649         if (!rc)
650         {
651             if (GetLastError() == ERROR_IO_PENDING)
652             {
653                 trace("InternetReadFileEx -> PENDING\n");
654                 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
655                 WaitForSingleObject(hCompleteEvent, INFINITE);
656                 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
657                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
658             }
659             else
660             {
661                 trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
662                 break;
663             }
664         }
665         else
666         {
667             trace("InternetReadFileEx -> SUCCEEDED\n");
668             CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
669             if (inetbuffers.dwBufferLength)
670             {
671                 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
672                 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
673             }
674             else
675             {
676                 /* Win98 still sends these when 0 bytes are read, WinXP does not */
677                 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
678                 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
679             }
680         }
681
682         trace("read %i bytes\n", inetbuffers.dwBufferLength);
683         ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
684
685         ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
686             "InternetReadFileEx sets offsets to 0x%x%08x\n",
687             inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
688
689         HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
690
691         if (!inetbuffers.dwBufferLength)
692             break;
693
694         length += inetbuffers.dwBufferLength;
695     }
696     ok(length > 0, "failed to read any of the document\n");
697     trace("Finished. Read %d bytes\n", length);
698
699     /* WinXP does not send, but Win98 does */
700     CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
701     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
702 abort:
703     SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
704     if (hor) {
705         rc = InternetCloseHandle(hor);
706         ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
707         rc = InternetCloseHandle(hor);
708         ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
709     }
710     if (hic) {
711         rc = InternetCloseHandle(hic);
712         ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed\n");
713     }
714     if (hi) {
715       SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
716       rc = InternetCloseHandle(hi);
717       ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
718       if (flags & INTERNET_FLAG_ASYNC)
719           Sleep(100);
720       todo_wine CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
721     }
722     CloseHandle(hCompleteEvent);
723     first_connection_to_test_url = FALSE;
724 }
725
726 static void InternetOpenUrlA_test(void)
727 {
728   HINTERNET myhinternet, myhttp;
729   char buffer[0x400];
730   DWORD size, readbytes, totalbytes=0;
731   BOOL ret;
732   
733   myhinternet = InternetOpen("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
734   ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
735   size = 0x400;
736   ret = InternetCanonicalizeUrl(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
737   ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
738
739   SetLastError(0);
740   myhttp = InternetOpenUrl(myhinternet, TEST_URL, 0, 0,
741                            INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
742   if (GetLastError() == 12007)
743     return; /* WinXP returns this when not connected to the net */
744   ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
745   ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
746   ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
747   totalbytes += readbytes;
748   while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
749     totalbytes += readbytes;
750   trace("read 0x%08x bytes\n",totalbytes);
751 }
752
753 static void InternetTimeFromSystemTimeA_test(void)
754 {
755     BOOL ret;
756     static const SYSTEMTIME time = { 2005, 1, 5, 7, 12, 6, 35, 0 };
757     char string[INTERNET_RFC1123_BUFSIZE];
758     static const char expect[] = "Fri, 07 Jan 2005 12:06:35 GMT";
759
760     ret = pInternetTimeFromSystemTimeA( &time, INTERNET_RFC1123_FORMAT, string, sizeof(string) );
761     ok( ret, "InternetTimeFromSystemTimeA failed (%u)\n", GetLastError() );
762
763     ok( !memcmp( string, expect, sizeof(expect) ),
764         "InternetTimeFromSystemTimeA failed (%u)\n", GetLastError() );
765 }
766
767 static void InternetTimeFromSystemTimeW_test(void)
768 {
769     BOOL ret;
770     static const SYSTEMTIME time = { 2005, 1, 5, 7, 12, 6, 35, 0 };
771     WCHAR string[INTERNET_RFC1123_BUFSIZE + 1];
772     static const WCHAR expect[] = { 'F','r','i',',',' ','0','7',' ','J','a','n',' ','2','0','0','5',' ',
773                                     '1','2',':','0','6',':','3','5',' ','G','M','T',0 };
774
775     ret = pInternetTimeFromSystemTimeW( &time, INTERNET_RFC1123_FORMAT, string, sizeof(string) );
776     ok( ret, "InternetTimeFromSystemTimeW failed (%u)\n", GetLastError() );
777
778     ok( !memcmp( string, expect, sizeof(expect) ),
779         "InternetTimeFromSystemTimeW failed (%u)\n", GetLastError() );
780 }
781
782 static void InternetTimeToSystemTimeA_test(void)
783 {
784     BOOL ret;
785     SYSTEMTIME time;
786     static const SYSTEMTIME expect = { 2005, 1, 5, 7, 12, 6, 35, 0 };
787     static const char string[] = "Fri, 07 Jan 2005 12:06:35 GMT";
788     static const char string2[] = " fri 7 jan 2005 12 06 35";
789
790     ret = pInternetTimeToSystemTimeA( string, &time, 0 );
791     ok( ret, "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
792     ok( !memcmp( &time, &expect, sizeof(expect) ),
793         "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
794
795     ret = pInternetTimeToSystemTimeA( string2, &time, 0 );
796     ok( ret, "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
797     ok( !memcmp( &time, &expect, sizeof(expect) ),
798         "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
799 }
800
801 static void InternetTimeToSystemTimeW_test(void)
802 {
803     BOOL ret;
804     SYSTEMTIME time;
805     static const SYSTEMTIME expect = { 2005, 1, 5, 7, 12, 6, 35, 0 };
806     static const WCHAR string[] = { 'F','r','i',',',' ','0','7',' ','J','a','n',' ','2','0','0','5',' ',
807                                     '1','2',':','0','6',':','3','5',' ','G','M','T',0 };
808     static const WCHAR string2[] = { ' ','f','r','i',' ','7',' ','j','a','n',' ','2','0','0','5',' ',
809                                      '1','2',' ','0','6',' ','3','5',0 };
810     static const WCHAR string3[] = { 'F','r',0 };
811
812     ret = pInternetTimeToSystemTimeW( NULL, NULL, 0 );
813     ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
814
815     ret = pInternetTimeToSystemTimeW( NULL, &time, 0 );
816     ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
817
818     ret = pInternetTimeToSystemTimeW( string, NULL, 0 );
819     ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
820
821     ret = pInternetTimeToSystemTimeW( string, &time, 0 );
822     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
823
824     ret = pInternetTimeToSystemTimeW( string, &time, 0 );
825     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
826     ok( !memcmp( &time, &expect, sizeof(expect) ),
827         "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
828
829     ret = pInternetTimeToSystemTimeW( string2, &time, 0 );
830     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
831     ok( !memcmp( &time, &expect, sizeof(expect) ),
832         "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
833
834     ret = pInternetTimeToSystemTimeW( string3, &time, 0 );
835     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
836 }
837
838 static void HttpSendRequestEx_test(void)
839 {
840     HINTERNET hSession;
841     HINTERNET hConnect;
842     HINTERNET hRequest;
843
844     INTERNET_BUFFERS BufferIn;
845     DWORD dwBytesWritten;
846     DWORD dwBytesRead;
847     CHAR szBuffer[256];
848     int i;
849     BOOL ret;
850
851     static char szPostData[] = "mode=Test";
852     static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
853
854     hSession = InternetOpen("Wine Regression Test",
855             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
856     ok( hSession != NULL ,"Unable to open Internet session\n");
857     hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
858             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
859             0);
860     ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
861     hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
862             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
863     if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
864     {
865         trace( "Network unreachable, skipping test\n" );
866         goto done;
867     }
868     ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
869
870
871     BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS);
872     BufferIn.Next = (LPINTERNET_BUFFERS)0xdeadcab;
873     BufferIn.lpcszHeader = szContentType;
874     BufferIn.dwHeadersLength = sizeof(szContentType);
875     BufferIn.dwHeadersTotal = sizeof(szContentType);
876     BufferIn.lpvBuffer = szPostData;
877     BufferIn.dwBufferLength = 3;
878     BufferIn.dwBufferTotal = sizeof(szPostData)-1;
879     BufferIn.dwOffsetLow = 0;
880     BufferIn.dwOffsetHigh = 0;
881
882     ret = HttpSendRequestEx(hRequest, &BufferIn, NULL, 0 ,0);
883     ok(ret, "HttpSendRequestEx Failed with error %u\n", GetLastError());
884
885     for (i = 3; szPostData[i]; i++)
886         ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
887                 "InternetWriteFile failed\n");
888
889     ok(HttpEndRequest(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
890
891     ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
892             "Unable to read response\n");
893     szBuffer[dwBytesRead] = 0;
894
895     ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
896     ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0,"Got string %s\n",szBuffer);
897
898     ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
899 done:
900     ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
901     ok(InternetCloseHandle(hSession), "Close session handle failed\n");
902 }
903
904 static void InternetOpenRequest_test(void)
905 {
906     HINTERNET session, connect, request;
907     static const char *types[] = { "*", "", NULL };
908     static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
909     static const WCHAR *typesW[] = { any, empty, NULL };
910     BOOL ret;
911
912     session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
913     ok(session != NULL ,"Unable to open Internet session\n");
914
915     connect = InternetConnectA(session, "winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
916                               INTERNET_SERVICE_HTTP, 0, 0);
917     ok(connect != NULL, "Unable to connect to http://winehq.org\n");
918
919     request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
920     if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
921     {
922         trace( "Network unreachable, skipping test\n" );
923         goto done;
924     }
925     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
926
927     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
928     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
929     ok(InternetCloseHandle(request), "Close request handle failed\n");
930
931     request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
932     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
933
934     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
935     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
936     ok(InternetCloseHandle(request), "Close request handle failed\n");
937
938 done:
939     ok(InternetCloseHandle(connect), "Close connect handle failed\n");
940     ok(InternetCloseHandle(session), "Close session handle failed\n");
941 }
942
943 static void HttpHeaders_test(void)
944 {
945     HINTERNET hSession;
946     HINTERNET hConnect;
947     HINTERNET hRequest;
948     CHAR      buffer[256];
949     DWORD     len = 256;
950     DWORD     index = 0;
951
952     hSession = InternetOpen("Wine Regression Test",
953             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
954     ok( hSession != NULL ,"Unable to open Internet session\n");
955     hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
956             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
957             0);
958     ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
959     hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
960             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
961     if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
962     {
963         trace( "Network unreachable, skipping test\n" );
964         goto done;
965     }
966     ok( hRequest != NULL, "Failed to open request handle\n");
967
968     index = 0;
969     len = sizeof(buffer);
970     strcpy(buffer,"Warning");
971     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
972                buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
973     
974     ok(HttpAddRequestHeaders(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
975             "Failed to add new header\n");
976
977     index = 0;
978     len = sizeof(buffer);
979     strcpy(buffer,"Warning");
980     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
981                 buffer,&len,&index),"Unable to query header\n");
982     ok(index == 1, "Index was not incremented\n");
983     ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
984     len = sizeof(buffer);
985     strcpy(buffer,"Warning");
986     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
987                 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
988
989     ok(HttpAddRequestHeaders(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
990             "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
991
992     index = 0;
993     len = sizeof(buffer);
994     strcpy(buffer,"Warning");
995     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
996                 buffer,&len,&index),"Unable to query header\n");
997     ok(index == 1, "Index was not incremented\n");
998     ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
999     len = sizeof(buffer);
1000     strcpy(buffer,"Warning");
1001     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1002                 buffer,&len,&index),"Failed to get second header\n");
1003     ok(index == 2, "Index was not incremented\n");
1004     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1005     len = sizeof(buffer);
1006     strcpy(buffer,"Warning");
1007     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1008                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1009
1010     ok(HttpAddRequestHeaders(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1011
1012     index = 0;
1013     len = sizeof(buffer);
1014     strcpy(buffer,"Warning");
1015     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1016                 buffer,&len,&index),"Unable to query header\n");
1017     ok(index == 1, "Index was not incremented\n");
1018     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1019     len = sizeof(buffer);
1020     strcpy(buffer,"Warning");
1021     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1022                 buffer,&len,&index),"Failed to get second header\n");
1023     ok(index == 2, "Index was not incremented\n");
1024     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1025     len = sizeof(buffer);
1026     strcpy(buffer,"Warning");
1027     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1028                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1029     
1030     ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1031
1032     index = 0;
1033     len = sizeof(buffer);
1034     strcpy(buffer,"Warning");
1035     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1036                 buffer,&len,&index),"Unable to query header\n");
1037     ok(index == 1, "Index was not incremented\n");
1038     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1039     len = sizeof(buffer);
1040     strcpy(buffer,"Warning");
1041     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1042                 buffer,&len,&index),"Failed to get second header\n");
1043     ok(index == 2, "Index was not incremented\n");
1044     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1045     len = sizeof(buffer);
1046     strcpy(buffer,"Warning");
1047     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1048                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1049
1050     ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1051
1052     index = 0;
1053     len = sizeof(buffer);
1054     strcpy(buffer,"Warning");
1055     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1056                 buffer,&len,&index),"Unable to query header\n");
1057     ok(index == 1, "Index was not incremented\n");
1058     ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
1059     len = sizeof(buffer);
1060     strcpy(buffer,"Warning");
1061     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1062     ok(index == 2, "Index was not incremented\n");
1063     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1064     len = sizeof(buffer);
1065     strcpy(buffer,"Warning");
1066     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1067
1068     ok(HttpAddRequestHeaders(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1069
1070     index = 0;
1071     len = sizeof(buffer);
1072     strcpy(buffer,"Warning");
1073     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1074     ok(index == 1, "Index was not incremented\n");
1075     ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
1076     len = sizeof(buffer);
1077     strcpy(buffer,"Warning");
1078     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1079     ok(index == 2, "Index was not incremented\n");
1080     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1081     len = sizeof(buffer);
1082     strcpy(buffer,"Warning");
1083     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1084
1085     ok(HttpAddRequestHeaders(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1086
1087     index = 0;
1088     len = sizeof(buffer);
1089     strcpy(buffer,"Warning");
1090     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1091     ok(index == 1, "Index was not incremented\n");
1092     ok(strcmp(buffer,"test2, test4, test5; test6")==0, "incorrect string was returned(%s)\n",buffer);
1093     len = sizeof(buffer);
1094     strcpy(buffer,"Warning");
1095     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1096     ok(index == 2, "Index was not incremented\n");
1097     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1098     len = sizeof(buffer);
1099     strcpy(buffer,"Warning");
1100     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1101
1102     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");
1103
1104     index = 0;
1105     len = sizeof(buffer);
1106     strcpy(buffer,"Warning");
1107     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1108     ok(index == 1, "Index was not incremented\n");
1109     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1110     len = sizeof(buffer);
1111     strcpy(buffer,"Warning");
1112     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1113     ok(index == 2, "Index was not incremented\n");
1114     ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
1115     len = sizeof(buffer);
1116     strcpy(buffer,"Warning");
1117     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1118
1119     
1120     ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1121 done:
1122     ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1123     ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1124 }
1125
1126 static const char okmsg[] =
1127 "HTTP/1.0 200 OK\r\n"
1128 "Server: winetest\r\n"
1129 "\r\n";
1130
1131 static const char proxymsg[] =
1132 "HTTP/1.1 407 Proxy Authentication Required\r\n"
1133 "Server: winetest\r\n"
1134 "Proxy-Connection: close\r\n"
1135 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
1136 "\r\n";
1137
1138 static const char page1[] =
1139 "<HTML>\r\n"
1140 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
1141 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
1142 "</HTML>\r\n\r\n";
1143
1144 struct server_info {
1145     HANDLE hEvent;
1146     int port;
1147 };
1148
1149 static DWORD CALLBACK server_thread(LPVOID param)
1150 {
1151     struct server_info *si = param;
1152     int r, s, c, i, on;
1153     struct sockaddr_in sa;
1154     char buffer[0x100];
1155     WSADATA wsaData;
1156     int last_request = 0;
1157
1158     WSAStartup(MAKEWORD(1,1), &wsaData);
1159
1160     s = socket(AF_INET, SOCK_STREAM, 0);
1161     if (s<0)
1162         return 1;
1163
1164     on = 1;
1165     setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
1166
1167     memset(&sa, 0, sizeof sa);
1168     sa.sin_family = AF_INET;
1169     sa.sin_port = htons(si->port);
1170     sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
1171
1172     r = bind(s, (struct sockaddr*) &sa, sizeof sa);
1173     if (r<0)
1174         return 1;
1175
1176     listen(s, 0);
1177
1178     SetEvent(si->hEvent);
1179
1180     do
1181     {
1182         c = accept(s, NULL, NULL);
1183
1184         memset(buffer, 0, sizeof buffer);
1185         for(i=0; i<(sizeof buffer-1); i++)
1186         {
1187             r = recv(c, &buffer[i], 1, 0);
1188             if (r != 1)
1189                 break;
1190             if (i<4) continue;
1191             if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
1192                 buffer[i-3] == '\r' && buffer[i-1] == '\r')
1193                 break;
1194         }
1195
1196         if (strstr(buffer, "GET /test1"))
1197         {
1198             send(c, okmsg, sizeof okmsg-1, 0);
1199             send(c, page1, sizeof page1-1, 0);
1200         }
1201
1202         if (strstr(buffer, "/test2"))
1203         {
1204             if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
1205             {
1206                 send(c, okmsg, sizeof okmsg-1, 0);
1207                 send(c, page1, sizeof page1-1, 0);
1208             }
1209             else
1210                 send(c, proxymsg, sizeof proxymsg-1, 0);
1211         }
1212
1213         if (strstr(buffer, "/quit"))
1214         {
1215             send(c, okmsg, sizeof okmsg-1, 0);
1216             send(c, page1, sizeof page1-1, 0);
1217             last_request = 1;
1218         }
1219
1220         shutdown(c, 2);
1221         closesocket(c);
1222     } while (!last_request);
1223
1224     closesocket(s);
1225
1226     return 0;
1227 }
1228
1229 static void test_basic_request(int port, const char *url)
1230 {
1231     HINTERNET hi, hc, hr;
1232     DWORD r, count;
1233     char buffer[0x100];
1234
1235     hi = InternetOpen(NULL, 0, NULL, NULL, 0);
1236     ok(hi != NULL, "open failed\n");
1237
1238     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1239     ok(hc != NULL, "connect failed\n");
1240
1241     hr = HttpOpenRequest(hc, NULL, url, NULL, NULL, NULL, 0, 0);
1242     ok(hr != NULL, "HttpOpenRequest failed\n");
1243
1244     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1245     ok(r, "HttpSendRequest failed\n");
1246
1247     count = 0;
1248     memset(buffer, 0, sizeof buffer);
1249     r = InternetReadFile(hr, buffer, sizeof buffer, &count);
1250     ok(r, "InternetReadFile failed\n");
1251     ok(count == sizeof page1 - 1, "count was wrong\n");
1252     ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
1253
1254     InternetCloseHandle(hr);
1255     InternetCloseHandle(hc);
1256     InternetCloseHandle(hi);
1257 }
1258
1259 static void test_proxy_indirect(int port)
1260 {
1261     HINTERNET hi, hc, hr;
1262     DWORD r, sz, val;
1263     char buffer[0x40];
1264
1265     hi = InternetOpen(NULL, 0, NULL, NULL, 0);
1266     ok(hi != NULL, "open failed\n");
1267
1268     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1269     ok(hc != NULL, "connect failed\n");
1270
1271     hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1272     ok(hr != NULL, "HttpOpenRequest failed\n");
1273
1274     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1275     ok(r, "HttpSendRequest failed\n");
1276
1277     sz = sizeof buffer;
1278     r = HttpQueryInfo(hr, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
1279     ok(r, "HttpQueryInfo failed\n");
1280     ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
1281
1282     sz = sizeof buffer;
1283     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1284     ok(r, "HttpQueryInfo failed\n");
1285     ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1286
1287     sz = sizeof val;
1288     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &val, &sz, NULL);
1289     ok(r, "HttpQueryInfo failed\n");
1290     ok(val == 407, "proxy code wrong\n");
1291
1292     sz = sizeof buffer;
1293     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
1294     ok(r, "HttpQueryInfo failed\n");
1295     ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
1296
1297     sz = sizeof buffer;
1298     r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &sz, NULL);
1299     ok(r, "HttpQueryInfo failed\n");
1300     ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
1301
1302     sz = sizeof buffer;
1303     r = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &sz, NULL);
1304     ok(r, "HttpQueryInfo failed\n");
1305     ok(!strcmp(buffer, "winetest"), "http server wrong\n");
1306
1307     sz = sizeof buffer;
1308     r = HttpQueryInfo(hr, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
1309     ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
1310     ok(r == FALSE, "HttpQueryInfo failed\n");
1311
1312     InternetCloseHandle(hr);
1313     InternetCloseHandle(hc);
1314     InternetCloseHandle(hi);
1315 }
1316
1317 static void test_proxy_direct(int port)
1318 {
1319     HINTERNET hi, hc, hr;
1320     DWORD r, sz;
1321     char buffer[0x40];
1322     static CHAR username[] = "mike",
1323                 password[] = "1101";
1324
1325     sprintf(buffer, "localhost:%d\n", port);
1326     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
1327     ok(hi != NULL, "open failed\n");
1328
1329     /* try connect without authorization */
1330     hc = InternetConnect(hi, "www.winehq.org/", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1331     ok(hc != NULL, "connect failed\n");
1332
1333     hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1334     ok(hr != NULL, "HttpOpenRequest failed\n");
1335
1336     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1337     ok(r, "HttpSendRequest failed\n");
1338
1339     sz = sizeof buffer;
1340     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1341     ok(r, "HttpQueryInfo failed\n");
1342     ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1343
1344
1345     /* set the user + password then try again */
1346     todo_wine {
1347     r = InternetSetOption(hr, INTERNET_OPTION_PROXY_USERNAME, username, 4);
1348     ok(r, "failed to set user\n");
1349
1350     r = InternetSetOption(hr, INTERNET_OPTION_PROXY_PASSWORD, password, 4);
1351     ok(r, "failed to set password\n");
1352     }
1353
1354     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1355     ok(r, "HttpSendRequest failed\n");
1356     sz = sizeof buffer;
1357     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1358     ok(r, "HttpQueryInfo failed\n");
1359     todo_wine {
1360     ok(!strcmp(buffer, "200"), "proxy code wrong\n");
1361     }
1362
1363
1364     InternetCloseHandle(hr);
1365     InternetCloseHandle(hc);
1366     InternetCloseHandle(hi);
1367 }
1368
1369 static void test_http_connection(void)
1370 {
1371     struct server_info si;
1372     HANDLE hThread;
1373     DWORD id = 0, r;
1374
1375     si.hEvent = CreateEvent(NULL, 0, 0, NULL);
1376     si.port = 7531;
1377
1378     hThread = CreateThread(NULL, 0, server_thread, (LPVOID) &si, 0, &id);
1379     ok( hThread != NULL, "create thread failed\n");
1380
1381     r = WaitForSingleObject(si.hEvent, 10000);
1382     ok (r == WAIT_OBJECT_0, "failed to start wininet test server\n");
1383     if (r != WAIT_OBJECT_0)
1384         return;
1385
1386     test_basic_request(si.port, "/test1");
1387     test_proxy_indirect(si.port);
1388     test_proxy_direct(si.port);
1389
1390     /* send the basic request again to shutdown the server thread */
1391     test_basic_request(si.port, "/quit");
1392
1393     r = WaitForSingleObject(hThread, 3000);
1394     ok( r == WAIT_OBJECT_0, "thread wait failed\n");
1395     CloseHandle(hThread);
1396 }
1397
1398 #define STATUS_STRING(status) \
1399     memcpy(status_string[status], #status, sizeof(CHAR) * \
1400            (strlen(#status) < MAX_STATUS_NAME ? \
1401             strlen(#status) : \
1402             MAX_STATUS_NAME - 1))
1403 static void init_status_tests(void)
1404 {
1405     memset(expect, 0, sizeof(expect));
1406     memset(wine_allow, 0, sizeof(wine_allow));
1407     memset(notified, 0, sizeof(notified));
1408     memset(status_string, 0, sizeof(status_string));
1409     STATUS_STRING(INTERNET_STATUS_RESOLVING_NAME);
1410     STATUS_STRING(INTERNET_STATUS_NAME_RESOLVED);
1411     STATUS_STRING(INTERNET_STATUS_CONNECTING_TO_SERVER);
1412     STATUS_STRING(INTERNET_STATUS_CONNECTED_TO_SERVER);
1413     STATUS_STRING(INTERNET_STATUS_SENDING_REQUEST);
1414     STATUS_STRING(INTERNET_STATUS_REQUEST_SENT);
1415     STATUS_STRING(INTERNET_STATUS_RECEIVING_RESPONSE);
1416     STATUS_STRING(INTERNET_STATUS_RESPONSE_RECEIVED);
1417     STATUS_STRING(INTERNET_STATUS_CTL_RESPONSE_RECEIVED);
1418     STATUS_STRING(INTERNET_STATUS_PREFETCH);
1419     STATUS_STRING(INTERNET_STATUS_CLOSING_CONNECTION);
1420     STATUS_STRING(INTERNET_STATUS_CONNECTION_CLOSED);
1421     STATUS_STRING(INTERNET_STATUS_HANDLE_CREATED);
1422     STATUS_STRING(INTERNET_STATUS_HANDLE_CLOSING);
1423     STATUS_STRING(INTERNET_STATUS_REQUEST_COMPLETE);
1424     STATUS_STRING(INTERNET_STATUS_REDIRECT);
1425     STATUS_STRING(INTERNET_STATUS_INTERMEDIATE_RESPONSE);
1426     STATUS_STRING(INTERNET_STATUS_USER_INPUT_REQUIRED);
1427     STATUS_STRING(INTERNET_STATUS_STATE_CHANGE);
1428     STATUS_STRING(INTERNET_STATUS_COOKIE_SENT);
1429     STATUS_STRING(INTERNET_STATUS_COOKIE_RECEIVED);
1430     STATUS_STRING(INTERNET_STATUS_PRIVACY_IMPACTED);
1431     STATUS_STRING(INTERNET_STATUS_P3P_HEADER);
1432     STATUS_STRING(INTERNET_STATUS_P3P_POLICYREF);
1433     STATUS_STRING(INTERNET_STATUS_COOKIE_HISTORY);
1434 }
1435 #undef STATUS_STRING
1436
1437 START_TEST(http)
1438 {
1439     HMODULE hdll;
1440     hdll = GetModuleHandleA("wininet.dll");
1441     pInternetSetStatusCallbackA = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackA");
1442     pInternetTimeFromSystemTimeA = (void*)GetProcAddress(hdll, "InternetTimeFromSystemTimeA");
1443     pInternetTimeFromSystemTimeW = (void*)GetProcAddress(hdll, "InternetTimeFromSystemTimeW");
1444     pInternetTimeToSystemTimeA = (void*)GetProcAddress(hdll, "InternetTimeToSystemTimeA");
1445     pInternetTimeToSystemTimeW = (void*)GetProcAddress(hdll, "InternetTimeToSystemTimeW");
1446
1447     if (!pInternetSetStatusCallbackA)
1448         skip("skipping the InternetReadFile tests\n");
1449     else
1450     {
1451         init_status_tests();
1452         InternetReadFile_test(INTERNET_FLAG_ASYNC);
1453         InternetReadFile_test(0);
1454         InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
1455     }
1456     InternetOpenRequest_test();
1457     InternetOpenUrlA_test();
1458     if (!pInternetTimeFromSystemTimeA)
1459         skip("skipping the InternetTime tests\n");
1460     else
1461     {
1462         InternetTimeFromSystemTimeA_test();
1463         InternetTimeFromSystemTimeW_test();
1464         InternetTimeToSystemTimeA_test();
1465         InternetTimeToSystemTimeW_test();
1466     }
1467     HttpSendRequestEx_test();
1468     HttpHeaders_test();
1469     test_http_connection();
1470 }