fdopen: don't rewind the file after creating the FILE* handle. Added
[wine] / dlls / wininet / tests / http.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "windef.h"
5 #include "winbase.h"
6 #include "wininet.h"
7
8 #include "wine/test.h"
9
10 int goon = 0;
11
12 VOID WINAPI callback(
13      HINTERNET hInternet,
14      DWORD dwContext,
15      DWORD dwInternetStatus,
16      LPVOID lpvStatusInformation,
17      DWORD dwStatusInformationLength
18 )
19 {
20     char name[124];
21
22     switch (dwInternetStatus)
23     {
24         case INTERNET_STATUS_RESOLVING_NAME:
25             strcpy(name,"INTERNET_STATUS_RESOLVING_NAME");
26             break;
27         case INTERNET_STATUS_NAME_RESOLVED:
28             strcpy(name,"INTERNET_STATUS_NAME_RESOLVED");
29             break;
30         case INTERNET_STATUS_CONNECTING_TO_SERVER:
31             strcpy(name,"INTERNET_STATUS_CONNECTING_TO_SERVER");
32             break;
33         case INTERNET_STATUS_CONNECTED_TO_SERVER:
34             strcpy(name,"INTERNET_STATUS_CONNECTED_TO_SERVER");
35             break;
36         case INTERNET_STATUS_SENDING_REQUEST:
37             strcpy(name,"INTERNET_STATUS_SENDING_REQUEST");
38             break;
39         case INTERNET_STATUS_REQUEST_SENT:
40             strcpy(name,"INTERNET_STATUS_REQUEST_SENT");
41             break;
42         case INTERNET_STATUS_RECEIVING_RESPONSE:
43             strcpy(name,"INTERNET_STATUS_RECEIVING_RESPONSE");
44             break;
45         case INTERNET_STATUS_RESPONSE_RECEIVED:
46             strcpy(name,"INTERNET_STATUS_RESPONSE_RECEIVED");
47             break;
48         case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
49             strcpy(name,"INTERNET_STATUS_CTL_RESPONSE_RECEIVED");
50             break;
51         case INTERNET_STATUS_PREFETCH:
52             strcpy(name,"INTERNET_STATUS_PREFETCH");
53             break;
54         case INTERNET_STATUS_CLOSING_CONNECTION:
55             strcpy(name,"INTERNET_STATUS_CLOSING_CONNECTION");
56             break;
57         case INTERNET_STATUS_CONNECTION_CLOSED:
58             strcpy(name,"INTERNET_STATUS_CONNECTION_CLOSED");
59             break;
60         case INTERNET_STATUS_HANDLE_CREATED:
61             strcpy(name,"INTERNET_STATUS_HANDLE_CREATED");
62             break;
63         case INTERNET_STATUS_HANDLE_CLOSING:
64             strcpy(name,"INTERNET_STATUS_HANDLE_CLOSING");
65             break;
66         case INTERNET_STATUS_REQUEST_COMPLETE:
67             strcpy(name,"INTERNET_STATUS_REQUEST_COMPLETE");
68             goon = 1;
69             break;
70         case INTERNET_STATUS_REDIRECT:
71             strcpy(name,"INTERNET_STATUS_REDIRECT");
72             break;
73         case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
74             strcpy(name,"INTERNET_STATUS_INTERMEDIATE_RESPONSE");
75             break;
76     }
77
78     trace("Callback %p 0x%lx %s(%li) %p %ld\n",hInternet,dwContext,name,dwInternetStatus,lpvStatusInformation,dwStatusInformationLength);
79 }
80
81 void winapi_test(int flags)
82 {
83     DWORD rc;
84     CHAR buffer[4000];
85     DWORD length;
86     DWORD out;
87     const char *types[2] = { "*", NULL };
88     HINTERNET hi,hic,hor;
89
90     trace("Starting with flags 0x%x\n",flags);
91
92     trace("InternetOpenA <--\n");
93     hi = InternetOpenA("",0x0,0x0,0x0,flags);
94     ok((hi != 0x0),"InternetOpen Failed");
95     trace("InternetOpenA -->\n");
96
97     if (hi == 0x0) goto abort;
98
99     InternetSetStatusCallback(hi,&callback);
100
101     trace("InternetConnectA <--\n");
102     hic=InternetConnectA(hi,"www.winehq.com",0x0,0x0,0x0,0x3,0x0,0xdeadbeef);
103     ok((hic != 0x0),"InternetConnect Failed");
104     trace("InternetConnectA -->\n");
105
106     if (hic == 0x0) goto abort;
107
108     trace("HttpOpenRequestA <--\n");
109     hor = HttpOpenRequestA(hic, "GET",
110                           "/about/",
111                           0x0,0x0,types,0x00400800,0xdeadbead);
112     if (hor == 0x0 && GetLastError() == 12007 /* ERROR_INTERNET_NAME_NOT_RESOLVED */) {
113         /*
114          * If the internet name can't be resolved we are probably behind
115          * a firewall or in some other way not directly connected to the
116          * Internet. Not enough reason to fail the test. Just ignore and
117          * abort.
118          */
119     } else  {
120         ok((hor != 0x0),"HttpOpenRequest Failed");
121     }
122     trace("HttpOpenRequestA -->\n");
123
124     if (hor == 0x0) goto abort;
125
126     trace("HttpSendRequestA -->\n");
127     rc = HttpSendRequestA(hor, "", 0xffffffff,0x0,0x0);
128     if (flags)
129         ok(((rc == 0)&&(GetLastError()==997)),
130             "Asyncronous HttpSendRequest NOT returning 0 with error 997");
131     else
132         ok((rc != 0), "Syncronous HttpSendRequest returning 0");
133     trace("HttpSendRequestA <--\n");
134
135     while ((flags)&&(!goon))
136         Sleep(100);
137
138     length = 4;
139     rc = InternetQueryOptionA(hor,0x17,&out,&length);
140     trace("Option 0x17 -> %li  %li\n",rc,out);
141
142     length = 100;
143     rc = InternetQueryOptionA(hor,0x22,buffer,&length);
144     trace("Option 0x22 -> %li  %s\n",rc,buffer);
145
146     length = 4000;
147     rc = HttpQueryInfoA(hor,0x16,buffer,&length,0x0);
148     buffer[length]=0;
149     trace("Option 0x16 -> %li  %s\n",rc,buffer);
150
151     length = 4000;
152     rc = InternetQueryOptionA(hor,0x22,buffer,&length);
153     buffer[length]=0;
154     trace("Option 0x22 -> %li  %s\n",rc,buffer);
155
156     length = 16;
157     rc = HttpQueryInfoA(hor,0x5,&buffer,&length,0x0);
158     trace("Option 0x5 -> %li  %s  (%li)\n",rc,buffer,GetLastError());
159
160     length = 100;
161     rc = HttpQueryInfoA(hor,0x1,buffer,&length,0x0);
162     buffer[length]=0;
163     trace("Option 0x1 -> %li  %s\n",rc,buffer);
164
165     length = 100;
166     trace("Entery Query loop\n");
167
168     while (length)
169     {
170
171         rc = InternetQueryDataAvailable(hor,&length,0x0,0x0);
172         ok((rc != 0),"InternetQueryDataAvailable failed");
173
174         if (length)
175         {
176             char *buffer;
177             buffer = (char*)HeapAlloc(GetProcessHeap(),0,length+1);
178
179             rc = InternetReadFile(hor,buffer,length,&length);
180
181             buffer[length]=0;
182
183             trace("ReadFile -> %li %li\n",rc,length);
184
185             HeapFree(GetProcessHeap(),0,buffer);
186         }
187     }
188 abort:
189     if (hor != 0x0) {
190         rc = InternetCloseHandle(hor);
191         ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed");
192     }
193     if (hor != 0x0) {
194         rc = InternetCloseHandle(hic);
195         ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed");
196     }
197     if (hi != 0x0) {
198       rc = InternetCloseHandle(hi);
199       ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed");
200       if (flags)
201           Sleep(100);
202     }
203 }
204
205
206 START_TEST(http)
207 {
208     winapi_test(0x10000000);
209     winapi_test(0x00000000);
210 }