@@ -553,6 +553,46 @@ defmodule Plug.StaticTest do
553553 end
554554 end
555555
556+ test "serves the range only if if-range matches the etag" do
557+ conn = call ( conn ( :get , "/public/fixtures/static.txt" ) )
558+ assert [ etag ] = get_resp_header ( conn , "etag" )
559+
560+ conn =
561+ conn ( :get , "/public/fixtures/static.txt" , [ ] )
562+ |> put_req_header ( "range" , "bytes=0-1" )
563+ |> put_req_header ( "if-range" , etag )
564+ |> call ( )
565+
566+ assert conn . status == 206
567+ assert conn . resp_body == "HE"
568+ assert get_resp_header ( conn , "content-range" ) == [ "bytes 0-1/5" ]
569+
570+ for if_range <- [ ~s( "other") , "W/" <> etag , "Sun, 25 Oct 2026 00:00:00 GMT" ] ,
571+ range <- [ "bytes=0-1" , "bytes=10-20" ] do
572+ conn =
573+ conn ( :get , "/public/fixtures/static.txt" , [ ] )
574+ |> put_req_header ( "range" , range )
575+ |> put_req_header ( "if-range" , if_range )
576+ |> call ( )
577+
578+ assert conn . status == 200 , "expected 200 for #{ if_range } and #{ range } "
579+ assert conn . resp_body == "HELLO"
580+ assert get_resp_header ( conn , "content-range" ) == [ ]
581+ end
582+ end
583+
584+ test "ignores range if if-range is given and there is no etag" do
585+ conn =
586+ conn ( :get , "/public/fixtures/static.txt?vsn=bar" , [ ] )
587+ |> put_req_header ( "range" , "bytes=0-1" )
588+ |> put_req_header ( "if-range" , ~s( "abc") )
589+ |> call ( )
590+
591+ assert conn . status == 200
592+ assert conn . resp_body == "HELLO"
593+ assert get_resp_header ( conn , "etag" ) == [ ]
594+ end
595+
556596 test "performs etag negotiation" do
557597 conn =
558598 conn ( :get , "/public/fixtures/static.txt" )
0 commit comments