File size: 3,088 Bytes
447ebeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Tests for the HTTP command group."""

import json

import pytest
import responses
from click.testing import CliRunner

from litellm.proxy.client.cli.commands.http import http


@pytest.fixture
def runner():
    """Create a CLI test runner."""
    return CliRunner()


@responses.activate
def test_request_get(runner):
    """Test making a GET request."""
    responses.add(
        responses.GET,
        "http://localhost:4000/models",
        json={"models": []},
        status=200,
    )
    result = runner.invoke(
        http,
        ["request", "GET", "/models"],
        obj={"base_url": "http://localhost:4000", "api_key": "sk-test-key"},
    )
    assert result.exit_code == 0
    assert "models" in result.output


@responses.activate
def test_request_post_with_json(runner):
    """Test making a POST request with JSON data."""
    responses.add(
        responses.POST,
        "http://localhost:4000/chat/completions",
        json={"choices": [{"message": {"content": "Hello!"}}]},
        status=200,
    )
    result = runner.invoke(
        http,
        [
            "request",
            "POST",
            "/chat/completions",
            "-j",
            '{"model": "gpt-4", "messages": [{"role": "user", "content": "Hello"}]}',
        ],
        obj={"base_url": "http://localhost:4000", "api_key": "sk-test-key"},
    )
    assert result.exit_code == 0
    assert "choices" in result.output


@responses.activate
def test_request_with_headers(runner):
    """Test making a request with custom headers."""
    responses.add(
        responses.GET,
        "http://localhost:4000/models",
        json={"models": []},
        status=200,
    )
    result = runner.invoke(
        http,
        [
            "request",
            "GET",
            "/models",
            "-H",
            "X-Custom-Header:value",
            "-H",
            "Accept:application/json",
        ],
        obj={"base_url": "http://localhost:4000", "api_key": "sk-test-key"},
    )
    assert result.exit_code == 0
    assert "models" in result.output


def test_request_invalid_json(runner):
    """Test error handling for invalid JSON data."""
    result = runner.invoke(
        http,
        [
            "request",
            "POST",
            "/chat/completions",
            "-j",
            '{"invalid": json}',  # Invalid JSON
        ],
        obj={"base_url": "http://localhost:4000", "api_key": "sk-test-key"},
    )
    assert result.exit_code == 2  # Click error code for invalid parameter
    assert "Invalid JSON format" in result.output


def test_request_invalid_header(runner):
    """Test error handling for invalid header format."""
    result = runner.invoke(
        http,
        [
            "request",
            "GET",
            "/models",
            "-H",
            "invalid-header",  # Invalid header format
        ],
        obj={"base_url": "http://localhost:4000", "api_key": "sk-test-key"},
    )
    assert result.exit_code == 2  # Click error code for invalid parameter
    assert "Invalid header format" in result.output