Skip to content

Commit d429335

Browse files
committed
Get stricter about acceptable HTTP statuses
1 parent d5abc21 commit d429335

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

autoload/proompter/callback/channel.vim

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030
" ```
3131
function! proompter#callback#channel#CompleteToHistory(api_response, configurations = g:proompter, state = g:proompter_state, ...) abort
3232
let l:http_response = proompter#parse#HTTPResponse(a:api_response)
33+
if l:http_response.status.code < 200 || l:http_response.status.code >= 300
34+
throw join([
35+
\ 'HTTP response not okay ->',
36+
\ l:http_response.status.code,
37+
\ l:http_response.status.text,
38+
\ ], ' ')
39+
endif
3340

3441
let l:entry = {
3542
\ 'model': l:http_response.body[-1].model,
@@ -106,6 +113,14 @@ function! proompter#callback#channel#StreamToMessages(api_response, configuratio
106113
\ }
107114

108115
let l:http_response = proompter#parse#HTTPResponse(a:api_response)
116+
if len(l:http_response.status) && (l:http_response.status.code < 200 || l:http_response.status.code >= 300)
117+
throw join([
118+
\ 'HTTP response not okay ->',
119+
\ l:http_response.status.code,
120+
\ l:http_response.status.text,
121+
\ ], ' ')
122+
endif
123+
109124
if len(l:http_response.body) <= 0
110125
" echoe 'Skipping HTTP response with empty body'
111126
return
@@ -218,6 +233,14 @@ function! proompter#callback#channel#StreamToBuffer(api_response, configurations
218233
\ }
219234

220235
let l:http_response = proompter#parse#HTTPResponse(a:api_response)
236+
if len(l:http_response.status) && (l:http_response.status.code < 200 || l:http_response.status.code >= 300)
237+
throw join([
238+
\ 'HTTP response not okay ->',
239+
\ l:http_response.status.code,
240+
\ l:http_response.status.text,
241+
\ ], ' ')
242+
endif
243+
221244
if len(l:http_response.body) <= 0
222245
" echoe 'Skipping HTTP response with empty body'
223246
return

tests/units/autoload_proompter_callback_channel.vader

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Execute (proompter#callback#channel#CompleteToHistory -- Handles well formated H
55
let separator = "\r\n"
66

77
let api_response = join([
8+
\ 'HTTP/1.1 200 OK',
89
\ 'Server: SimpleHTTP/0.6 Python/3.12.6',
910
\ 'Date: Sat, 28 Sep 2024 23:29:00 GMT',
1011
\ 'Content-Type: application/json',
@@ -56,6 +57,31 @@ Execute (proompter#callback#channel#CompleteToHistory -- Handles well formated H
5657
AssertEqual state, expected_state
5758
AssertEqual configurations, expected_configurations, 'Unexpected mutation of configurations'
5859

60+
Execute (proompter#callback#channel#CompleteToHistory -- Throws when status code is not in 200 range):
61+
let separator = "\r\n"
62+
63+
let api_response = join([
64+
\ 'HTTP/1.1 500 Internal Server Error',
65+
\ 'Server: SimpleHTTP/0.6 Python/3.12.6',
66+
\ ], separator)
67+
68+
let configurations = {
69+
\ 'select': {
70+
\ 'model_name': 'mistral',
71+
\ },
72+
\ }
73+
74+
let expected_configurations = deepcopy(configurations)
75+
76+
let state = { 'messages': [] }
77+
78+
let expected_state = { 'messages': [ ] }
79+
80+
AssertThrows call proompter#callback#channel#CompleteToHistory(g:api_response, g:configurations, g:state)
81+
82+
AssertEqual g:vader_exception, 'HTTP response not okay -> 500 Internal Server Error'
83+
AssertEqual state, expected_state, 'Unexpected mutation of state'
84+
AssertEqual configurations, expected_configurations, 'Unexpected mutation of configurations'
5985
" }}}
6086
""
6187

@@ -65,6 +91,7 @@ Execute (proompter#callback#channel#StreamToMessages -- Handles JSON stream with
6591
let separator = "\r\n"
6692

6793
let api_response = join([
94+
\ 'HTTP/1.1 200 OK',
6895
\ 'Server: SimpleHTTP/0.6 Python/3.12.6',
6996
\ 'Date: Fri, 20 Sep 2024 23:25:06 GMT',
7097
\ 'Content-Type: application/json',
@@ -140,6 +167,32 @@ Execute (proompter#callback#channel#StreamToMessages -- Handles JSON stream with
140167

141168
AssertEqual state, expected_state
142169
AssertEqual configurations, expected_configurations, 'Unexpected mutation of configurations'
170+
171+
Execute (proompter#callback#channel#StreamToMessages -- Throws when status code is not in 200 range):
172+
let separator = "\r\n"
173+
174+
let api_response = join([
175+
\ 'HTTP/1.1 500 Internal Server Error',
176+
\ 'Server: SimpleHTTP/0.6 Python/3.12.6',
177+
\ ], separator)
178+
179+
let configurations = {
180+
\ 'select': {
181+
\ 'model_name': 'mistral',
182+
\ },
183+
\ }
184+
185+
let expected_configurations = deepcopy(configurations)
186+
187+
let state = { 'messages': [] }
188+
189+
let expected_state = { 'messages': [ ] }
190+
191+
AssertThrows call proompter#callback#channel#StreamToMessages(g:api_response, g:configurations, g:state)
192+
193+
AssertEqual g:vader_exception, 'HTTP response not okay -> 500 Internal Server Error'
194+
AssertEqual state, expected_state, 'Unexpected mutation of state'
195+
AssertEqual configurations, expected_configurations, 'Unexpected mutation of configurations'
143196
" }}}
144197
""
145198

@@ -149,6 +202,7 @@ Execute (proompter#callback#channel#StreamToBuffer -- Handles JSON stream with o
149202
let separator = "\r\n"
150203

151204
let api_response = join([
205+
\ 'HTTP/1.1 200 OK',
152206
\ 'Server: SimpleHTTP/0.6 Python/3.12.6',
153207
\ 'Date: Fri, 20 Sep 2024 23:25:06 GMT',
154208
\ 'Content-Type: application/json',
@@ -234,6 +288,7 @@ Execute(proompter#callback#channel#StreamToBuffer -- Handles JSON with newlines)
234288
let separator = "\r\n"
235289

236290
let api_response = join([
291+
\ 'HTTP/1.1 200 OK',
237292
\ 'Server: SimpleHTTP/0.6 Python/3.12.6',
238293
\ 'Date: Fri, 20 Sep 2024 23:25:06 GMT',
239294
\ 'Content-Type: application/json',
@@ -297,6 +352,37 @@ Execute(proompter#callback#channel#StreamToBuffer -- Handles JSON with newlines)
297352
AssertEqual state, expected_state
298353
AssertEqual configurations, expected_configurations, 'Unexpected mutation of configurations'
299354
AssertEqual getbufline(bufnr('[Vader-workbench]'), 0, '$')[-5:-3], split(expected_state.messages[-1].message.content, "\n")
355+
356+
Execute (proompter#callback#channel#StreamToBuffer -- Throws when status code is not in 200 range):
357+
let separator = "\r\n"
358+
359+
let api_response = join([
360+
\ 'HTTP/1.1 500 Internal Server Error',
361+
\ 'Server: SimpleHTTP/0.6 Python/3.12.6',
362+
\ ], separator)
363+
364+
let configurations = {
365+
\ 'select': {
366+
\ 'model_name': 'mistral',
367+
\ },
368+
\ }
369+
370+
let expected_configurations = deepcopy(configurations)
371+
372+
let state = { 'messages': [] }
373+
374+
let expected_state = { 'messages': [ ] }
375+
376+
AssertThrows call proompter#callback#channel#StreamToBuffer(
377+
\ g:api_response,
378+
\ g:configurations,
379+
\ g:state,
380+
\ '[Vader-workbench]',
381+
\ )
382+
383+
AssertEqual g:vader_exception, 'HTTP response not okay -> 500 Internal Server Error'
384+
AssertEqual state, expected_state, 'Unexpected mutation of state'
385+
AssertEqual configurations, expected_configurations, 'Unexpected mutation of configurations'
300386
" }}}
301387
""
302388

0 commit comments

Comments
 (0)