robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local n =require ( 'test.functional.testnvim' )() 2local t =require ( 'test.testutil' ) 3local describe , it , before_each =t . describe , t . it , t . before_each 4local skip_integ =os . getenv ( 'NVIM_TEST_INTEG' ) ~= '1' 5 6local exec_lua =n . exec_lua 7 8-- Serve one request and record what curl sent. 9local function http_server ( body , status ) 10t . skip ( n . fn . executable ( 'curl' ) == 0 , 'curl not found' ) 11return exec_lua ( function () 12vim . env . no_proxy ='127.0.0.1' 13local server =assert ( vim . uv . new_tcp ()) 14assert ( server : bind ( '127.0.0.1' , 0 )) 15assert ( server : listen ( 1 , function ( err ) 16assert ( not err , err ) 17local client =assert ( vim . uv . new_tcp ()) 18assert ( server : accept ( client )) 19server : close () 20local data ='' 21client : read_start ( function ( read_err , chunk ) 22assert ( not read_err , read_err ) 23assert ( chunk , 'Connection closed before the request was complete' ) 24data =data .. chunk 25-- TCP can split the headers and body across reads. 26local header_end =data : find ( '\r\n\r\n' , 1 , true ) 27if not header_end then 28return 29end 30local headers ={} 31for name , value in data : sub ( 1 , header_end + 1 ): gmatch ( '\r\n([^:]+):[ \t]*([^\r\n]*)' ) do 32headers [ name : lower ()] =value 33end 34local request_body =data : sub ( header_end + 4 ) 35if # request_body < ( tonumber ( headers [ 'content-length' ]) or 0 ) then 36return 37end 38client : read_stop () 39local method =data : match ( '^(%S+)' ) 40_G . http_request ={ method =method , headers =headers , body =request_body } 41local response =( 'HTTP/1.1 %s\r\nContent-Length: %d\r\nConnection: close\r\n\r\n%s' ): format ( 42status or '200 OK' , 43# body , 44method == 'HEAD' and '' or body 45) 46client : write ( response , function ( write_err ) 47assert ( not write_err , write_err ) 48client : close () 49end ) 50end ) 51end )) 52return ( 'http://127.0.0.1:%d/' ): format ( server : getsockname (). port ) 53end ) 54end 55 56---@param method vim.net.HttpMethod 57---@param url string 58---@param opts? vim.net.request.Opts 59---@return table 60local function request ( method , url , opts ) 61exec_lua ( function () 62vim . net . request ( method , url , opts , function ( err , res ) 63vim . rpcnotify ( 1 , 'response' , { error =err , response =res and res . body } ) 64end ) 65end ) 66 67local msg =assert ( n . next_msg (), 'Timed out waiting for HTTP response' ) 68t . eq ( { 'notification' , 'response' } , { msg [ 1 ], msg [ 2 ] } ) 69return msg [ 3 ][ 1 ] 70end 71 72describe ( 'vim.net.request' , function () 73before_each ( function () 74n : clear () 75end ) 76 77it ( 'fetches a URL into memory (async success)' , function () 78---@type table 79local result =request ( 'GET' , http_server ( 'hello' )) 80 81t . eq ( nil , result . error ) 82t . eq ( 'hello' , result . response ) 83end ) 84 85it ( "detects filetype, sets 'nomodified'" , function () 86t . skip ( skip_integ , 'NVIM_TEST_INTEG not set (network integration test)' ) 87 88local rv =exec_lua ( function () 89vim . cmd ( 'runtime! plugin/nvim/net.lua' ) 90vim . cmd ( 'runtime! filetype.lua' ) 91-- github raw dump of a small lua file in the neovim repo 92vim . cmd ( 93'edit https://raw.githubusercontent.com/neovim/neovim/master/runtime/syntax/tutor.lua' 94) 95vim . wait ( 2000 , function () 96return vim . bo . filetype ~= '' 97end ) 98-- wait for buffer to have content 99vim . wait ( 2000 , function () 100return vim . fn . wordcount (). bytes > 0 101end ) 102vim . wait ( 2000 , function () 103return vim . bo . modified == false 104end ) 105return { vim . bo . filetype , vim . bo . modified } 106end ) 107 108t . eq ( 'lua' , rv [ 1 ]) 109t . eq ( false , rv [ 2 ], 'Expected buffer to be unmodified for remote content' ) 110end ) 111 112it ( 'calls on_response with error on 404 (async failure)' , function () 113local result =request ( 'GET' , http_server ( '' , '404 Not Found' )) 114t . matches ( '404' , result . error ) 115end ) 116 117it ( 'plugin writes output to buffer' , function () 118local url =http_server ( '<html>test</html>' ) 119 120local content =exec_lua ( function () 121---@type string[] 122local lines 123 124local buf =vim . api . nvim_create_buf ( false , true ) 125---@diagnostic disable-next-line: param-type-mismatch 126vim . net . request ( url , { outbuf =buf } ) 127 128vim . wait ( 2000 , function () 129lines =vim . api . nvim_buf_get_lines ( buf , 0 , - 1 , false ) 130return lines [ 1 ] ~= '' 131end ) 132 133return lines 134end ) 135assert ( content and content [ 1 ]: find ( 'html' )) 136end ) 137 138it ( 'works with :read' , function () 139t . skip ( skip_integ , 'NVIM_TEST_INTEG not set (network integration test)' ) 140 141local content =exec_lua ( function () 142vim . cmd ( 'runtime plugin/net.lua' ) 143---@type string[] 144local lines 145 146vim . api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , { 'Here is some text' } ) 147vim . cmd ( ':read https://example.com' ) 148 149vim . wait ( 2000 , function () 150lines =vim . api . nvim_buf_get_lines ( 0 , 0 , - 1 , false ) 151return # lines > 1 152end ) 153 154return lines 155end ) 156 157t . eq ( true , content ~= nil ) 158t . eq ( true , content [ 1 ]: find ( 'Here' ) ~= nil ) 159t . eq ( true , content [ 2 ]: find ( 'html' ) ~= nil ) 160end ) 161 162it ( 'opens remote tar.gz URLs as tar archives' , function () 163t . skip ( skip_integ , 'NVIM_TEST_INTEG not set (network integration test)' ) 164 165local rv =exec_lua ( function () 166vim . cmd ( 'runtime! plugin/net.lua' ) 167vim . cmd ( 'runtime! plugin/tarPlugin.vim' ) 168 169vim . cmd ( 170'edit https://github.com/neovim/neovim/releases/download/nightly/nvim-macos-x86_64.tar.gz' 171) 172 173vim . wait ( 2500 , function () 174return vim . bo . filetype == 'tar' or vim . b . tarfile ~= nil 175end ) 176 177return { 178filetype =vim . bo . filetype , 179modified =vim . bo . modified , 180tarfile =vim . b . tarfile ~= nil , 181} 182end ) 183 184t . eq ( 'tar' , rv . filetype ) 185t . eq ( false , rv . modified ) 186t . eq ( true , rv . tarfile ) 187end ) 188 189it ( 'dispatches remote zip URLs to zip.lua' , function () 190n . clear ( { args_rm ={ '-u' } } ) 191local fixture = 192vim . fs . joinpath ( t . paths . test_source_path , 'test/functional/fixtures/zip/browser.zip' ) 193local rv =exec_lua ( function ( path ) 194vim . net . request =function ( _ , opts , callback ) 195assert ( vim . uv . fs_copyfile ( path , opts . outpath )) 196callback ( nil ) 197end 198vim . cmd . edit ( 'https://example.com/browser.zip' ) 199vim . wait ( 1000 , function () 200return vim . b . nvim_zip ~= nil 201end ) 202require ( 'nvim.zip' ). open_parent ( 0 , vim . api . nvim_buf_get_name ( 0 )) 203return { 204legacy =vim . g . loaded_zip ~= nil , 205name =vim . api . nvim_buf_get_name ( 0 ), 206nvim_zip =vim . b . nvim_zip ~= nil , 207} 208end , fixture ) 209 210t . eq ( false , rv . legacy ) 211t . eq ( 'https://example.com/browser.zip' , rv . name ) 212t . eq ( true , rv . nvim_zip ) 213end ) 214 215it ( 'downloads a live remote zip URL' , function () 216t . skip ( skip_integ , 'NVIM_TEST_INTEG not set (network integration test)' ) 217n . clear ( { args ={ '--clean' } } ) 218 219local rv =exec_lua ( function () 220vim . cmd ( 'edit https://github.com/neovim/neovim/releases/download/nightly/nvim-win-arm64.zip' ) 221 222vim . wait ( 2500 , function () 223return vim . bo . filetype == 'zip' or vim . b . zipfile ~= nil 224end ) 225 226require ( 'nvim.zip' ). open_parent ( 0 , vim . api . nvim_buf_get_name ( 0 )) 227return { 228filetype =vim . bo . filetype , 229legacy =vim . g . loaded_zip ~= nil , 230modified =vim . bo . modified , 231name =vim . api . nvim_buf_get_name ( 0 ), 232nvim_zip =vim . b . nvim_zip ~= nil , 233zipfile =vim . b . zipfile ~= nil , 234} 235end ) 236 237t . eq ( 'zip' , rv . filetype ) 238t . eq ( false , rv . legacy ) 239t . eq ( false , rv . modified ) 240t . eq ( 'https://github.com/neovim/neovim/releases/download/nightly/nvim-win-arm64.zip' , rv . name ) 241t . eq ( true , rv . nvim_zip ) 242t . eq ( false , rv . zipfile ) 243end ) 244 245it ( 'accepts custom headers' , function () 246---@type table 247local result =request ( 'GET' , http_server ( 'hello' ), { 248headers ={ 249Authorization ='Bearer test-token' , 250[ 'X-Custom-Header' ] ='custom-value' , 251[ 'Empty' ] ='' , 252} , 253} ) 254 255t . eq ( nil , result . error ) 256local headers =exec_lua ( 'return http_request.headers' ) 257t . eq ( 'Bearer test-token' , headers . authorization , 'Expected Authorization header' ) 258t . eq ( 'custom-value' , headers [ 'x-custom-header' ], 'Expected X-Custom-Header' ) 259t . eq ( '' , headers . empty , 'Expected Empty header' ) 260end ) 261 262it ( 'accepts multiple HTTP methods' , function () 263local function assert_accept_method ( method ) 264local result =request ( method , http_server ( 'hello' )) 265t . eq ( nil , result . error ) 266t . eq ( method , exec_lua ( 'return http_request.method' )) 267end 268 269assert_accept_method ( 'GET' ) 270assert_accept_method ( 'PUT' ) 271assert_accept_method ( 'PATCH' ) 272assert_accept_method ( 'DELETE' ) 273 274-- HEAD request 275local result =request ( 'HEAD' , http_server ( 'hello' )) 276t . eq ( nil , result . error ) 277t . eq ( 'HEAD' , exec_lua ( 'return http_request.method' )) 278 279-- testing body payload 280result =request ( 'POST' , http_server ( 'hello' ), { 281body ='{"a": 1}' , 282headers ={ 283[ 'Content-Type' ] ='application/json' , 284} , 285} ) 286t . eq ( nil , result . error ) 287local sent =exec_lua ( 'return http_request' ) 288t . eq ( 'POST' , sent . method ) 289t . eq ( 'application/json' , sent . headers [ 'content-type' ]) 290t . eq ( '{"a": 1}' , sent . body ) 291end ) 292 293it ( 'validation' , function () 294local function assert_wrong_request ( expected_err , method , opts ) 295if type ( method ) ~= 'string' then 296opts =method 297method ='GET' 298end 299 300local result =t . pcall_err ( exec_lua , function () 301vim . net . request ( method , 'https://example.com' , opts ) 302end ) 303t . matches ( expected_err , result ) 304end 305 306-- headers asserts 307assert_wrong_request ( 'opts.headers: expected table, got number' , { headers =123 } ) 308 309--- FIXME(ellisonleao): this special assert is failing because the opts table is putting [""] in 310--- the key value instead of [123] upon calling the helper method 311-- assert_wrong_request( 312-- 'headers keys and values must be strings', 313-- { headers = { [123] = 'value' } } 314-- ) 315 316assert_wrong_request ( 'headers keys and values must be strings' , { headers ={ Header =123 } } ) 317assert_wrong_request ( 318'header keys must not start with @ or end with : and ;' , 319{ headers ={ [ 'Header:' ] ='value' } } 320) 321assert_wrong_request ( 322'header keys must not start with @ or end with : and ;' , 323{ headers ={ [ 'Header;' ] ='value' } } 324) 325assert_wrong_request ( 326'header keys must not start with @ or end with : and ;' , 327{ headers ={ [ '@filename' ] ='' } } 328) 329 330-- body asserts 331assert_wrong_request ( 332'opts.body: expected body should be string and not start with @' , 333{ body =123 } 334) 335assert_wrong_request ( 336'opts.body: expected body should be string and not start with @' , 337{ body ={} } 338) 339assert_wrong_request ( 340'opts.body: expected body should be string and not start with @' , 341{ body ='@test' } 342) 343 344-- OPTIONS is not accepted 345assert_wrong_request ( 346'expected method should be one of GET, POST, PUT, PATCH, HEAD, DELETE, got OPTIONS' , 347'OPTIONS' 348) 349-- lowercase methods are not accepted as well 350assert_wrong_request ( 351'expected method should be one of GET, POST, PUT, PATCH, HEAD, DELETE, got options' , 352'options' 353) 354assert_wrong_request ( 355'expected method should be one of GET, POST, PUT, PATCH, HEAD, DELETE, got get' , 356'get' 357) 358end ) 359end )