MCPcopy
hub / github.com/valyala/fasthttp / TestRequestCtxSendFileModified

Function TestRequestCtxSendFileModified

server_test.go:3071–3112  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

3069}
3070
3071func TestRequestCtxSendFileModified(t *testing.T) {
3072 t.Parallel()
3073
3074 var ctx RequestCtx
3075 var req Request
3076 ctx.Init(&req, nil, defaultLogger)
3077
3078 filePath := "./server_test.go"
3079 lastModified, err := FileLastModified(filePath)
3080 if err != nil {
3081 t.Fatalf("unexpected error: %v", err)
3082 }
3083 lastModified = lastModified.Add(-time.Hour)
3084 ctx.Request.Header.Set("If-Modified-Since", string(AppendHTTPDate(nil, lastModified)))
3085
3086 ctx.SendFile(filePath)
3087
3088 s := ctx.Response.String()
3089
3090 var resp Response
3091 br := bufio.NewReader(bytes.NewBufferString(s))
3092 if err := resp.Read(br); err != nil {
3093 t.Fatalf("error when reading response: %v", err)
3094 }
3095 if resp.StatusCode() != StatusOK {
3096 t.Fatalf("unexpected status code: %d. Expecting %d", resp.StatusCode(), StatusOK)
3097 }
3098
3099 f, err := os.Open(filePath)
3100 if err != nil {
3101 t.Fatalf("cannot open file: %v", err)
3102 }
3103 body, err := io.ReadAll(f)
3104 f.Close()
3105 if err != nil {
3106 t.Fatalf("error when reading file: %v", err)
3107 }
3108
3109 if !bytes.Equal(resp.Body(), body) {
3110 t.Fatalf("unexpected response body: %q. Expecting %q", resp.Body(), body)
3111 }
3112}
3113
3114func TestRequestCtxSendFile(t *testing.T) {
3115 t.Parallel()

Callers

nothing calls this directly

Calls 13

InitMethod · 0.95
SendFileMethod · 0.95
ReadMethod · 0.95
StatusCodeMethod · 0.95
BodyMethod · 0.95
FileLastModifiedFunction · 0.85
AppendHTTPDateFunction · 0.85
NewReaderMethod · 0.80
OpenMethod · 0.80
CloseMethod · 0.65
AddMethod · 0.45
SetMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…