File: test_16_info.py

package info (click to toggle)
curl 8.19.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 31,892 kB
  • sloc: ansic: 200,254; perl: 21,116; python: 10,390; sh: 6,691; makefile: 1,505; pascal: 240; cpp: 196
file content (123 lines) | stat: -rw-r--r-- 5,606 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#***************************************************************************
#                                  _   _ ____  _
#  Project                     ___| | | |  _ \| |
#                             / __| | | | |_) | |
#                            | (__| |_| |  _ <| |___
#                             \___|\___/|_| \_\_____|
#
# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at https://curl.se/docs/copyright.html.
#
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
# copies of the Software, and permit persons to whom the Software is
# furnished to do so, under the terms of the COPYING file.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
# SPDX-License-Identifier: curl
#
###########################################################################
#
import logging
import os
import pytest

from testenv import Env, CurlClient


log = logging.getLogger(__name__)


class TestInfo:

    @pytest.fixture(autouse=True, scope='class')
    def _class_scope(self, env, httpd):
        indir = httpd.docs_dir
        env.make_data_file(indir=indir, fname="data-10k", fsize=10*1024)
        env.make_data_file(indir=indir, fname="data-100k", fsize=100*1024)
        env.make_data_file(indir=indir, fname="data-1m", fsize=1024*1024)
        env.make_data_file(indir=env.gen_dir, fname="data-100k", fsize=100*1024)

    # download plain file
    @pytest.mark.parametrize("proto", Env.http_protos())
    def test_16_01_info_download(self, env: Env, httpd, nghttpx, proto):
        count = 2
        curl = CurlClient(env=env)
        url = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-{count-1}]'
        r = curl.http_download(urls=[url], alpn_proto=proto, with_stats=True)
        r.check_stats(count=count, http_status=200, exitcode=0,
                      remote_port=env.port_for(alpn_proto=proto),
                      remote_ip='127.0.0.1')
        for idx, s in enumerate(r.stats):
            self.check_stat(idx, s, r, dl_size=30, ul_size=0)
            r.check_stats_timeline(idx)

    # download plain file with a 302 redirect
    @pytest.mark.parametrize("proto", Env.http_protos())
    def test_16_02_info_302_download(self, env: Env, httpd, nghttpx, proto):
        count = 2
        curl = CurlClient(env=env)
        url = f'https://{env.authority_for(env.domain1, proto)}/data.json.302?[0-{count-1}]'
        r = curl.http_download(urls=[url], alpn_proto=proto, with_stats=True, extra_args=[
            '--location'
        ])
        r.check_stats(count=count, http_status=200, exitcode=0,
                      remote_port=env.port_for(alpn_proto=proto),
                      remote_ip='127.0.0.1')
        for idx, s in enumerate(r.stats):
            self.check_stat(idx, s, r, dl_size=30, ul_size=0)
            r.check_stats_timeline(idx)

    @pytest.mark.parametrize("proto", Env.http_protos())
    def test_16_03_info_upload(self, env: Env, httpd, nghttpx, proto):
        count = 2
        fdata = os.path.join(env.gen_dir, 'data-100k')
        fsize = 100 * 1024
        curl = CurlClient(env=env)
        url = f'https://{env.authority_for(env.domain1, proto)}/curltest/echo?id=[0-{count-1}]'
        r = curl.http_upload(urls=[url], data=f'@{fdata}', alpn_proto=proto,
                             with_headers=True, extra_args=[
                                '--trace-config', 'http/2,http/3'
                             ])
        r.check_response(count=count, http_status=200)
        r.check_stats(count=count, http_status=200, exitcode=0,
                      remote_port=env.port_for(alpn_proto=proto),
                      remote_ip='127.0.0.1')
        for idx, s in enumerate(r.stats):
            self.check_stat(idx, s, r, dl_size=fsize, ul_size=fsize)
            r.check_stats_timeline(idx)

    # download plain file via http: ('time_appconnect' is 0)
    @pytest.mark.parametrize("proto", ['http/1.1'])
    def test_16_04_info_http_download(self, env: Env, httpd, nghttpx, proto):
        count = 2
        curl = CurlClient(env=env)
        url = f'http://{env.domain1}:{env.http_port}/data.json?[0-{count-1}]'
        r = curl.http_download(urls=[url], alpn_proto=proto, with_stats=True)
        r.check_stats(count=count, http_status=200, exitcode=0,
                      remote_port=env.http_port, remote_ip='127.0.0.1')
        for idx, s in enumerate(r.stats):
            self.check_stat(idx, s, r, dl_size=30, ul_size=0)
            r.check_stats_timeline(idx)

    def check_stat(self, idx, s, r, dl_size=None, ul_size=None):
        # we always send something
        self.check_stat_positive(s, idx, 'size_request')
        # we always receive response headers
        self.check_stat_positive(s, idx, 'size_header')
        if ul_size is not None:
            assert s['size_upload'] == ul_size, f'stat #{idx}\n{r.dump_logs()}'  # the file we sent
        assert s['size_request'] >= s['size_upload'], \
            f'stat #{idx}, "size_request" smaller than "size_upload", {s}\n{r.dump_logs()}'
        if dl_size is not None:
            assert s['size_download'] == dl_size, f'stat #{idx}\n{r.dump_logs()}'  # the file we received

    def check_stat_positive(self, s, idx, key):
        assert key in s, f'stat #{idx} "{key}" missing: {s}'
        assert s[key] > 0, f'stat #{idx} "{key}" not positive: {s}'