xref: /third_party/curl/tests/libtest/lib1532.c (revision 13498266)
1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24#include "test.h"
25
26#include "memdebug.h"
27
28/* Test CURLINFO_RESPONSE_CODE */
29
30int test(char *URL)
31{
32  CURL *curl;
33  long httpcode;
34  CURLcode res = CURLE_OK;
35
36  global_init(CURL_GLOBAL_ALL);
37
38  easy_init(curl);
39
40  easy_setopt(curl, CURLOPT_URL, URL);
41
42  res = curl_easy_perform(curl);
43  if(res) {
44    fprintf(stderr, "%s:%d curl_easy_perform() failed with code %d (%s)\n",
45            __FILE__, __LINE__, res, curl_easy_strerror(res));
46    goto test_cleanup;
47  }
48
49  res = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpcode);
50  if(res) {
51    fprintf(stderr, "%s:%d curl_easy_getinfo() failed with code %d (%s)\n",
52            __FILE__, __LINE__, res, curl_easy_strerror(res));
53    goto test_cleanup;
54  }
55  if(httpcode != 200) {
56    fprintf(stderr, "%s:%d unexpected response code %ld\n",
57            __FILE__, __LINE__, httpcode);
58    res = CURLE_HTTP_RETURNED_ERROR;
59    goto test_cleanup;
60  }
61
62  /* Test for a regression of github bug 1017 (response code does not reset) */
63  curl_easy_reset(curl);
64
65  res = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpcode);
66  if(res) {
67    fprintf(stderr, "%s:%d curl_easy_getinfo() failed with code %d (%s)\n",
68            __FILE__, __LINE__, res, curl_easy_strerror(res));
69    goto test_cleanup;
70  }
71  if(httpcode) {
72    fprintf(stderr, "%s:%d curl_easy_reset failed to zero the response code\n"
73            "possible regression of github bug 1017\n", __FILE__, __LINE__);
74    res = CURLE_HTTP_RETURNED_ERROR;
75    goto test_cleanup;
76  }
77
78test_cleanup:
79  curl_easy_cleanup(curl);
80  curl_global_cleanup();
81  return (int)res;
82}
83