httpfile - examples_test.go
1 package httpfile_test
2
3 import (
4 "bytes"
5 _ "embed"
6 "fmt"
7 "net/http"
8 "net/http/httptest"
9
10 "vimagination.zapto.org/httpfile"
11 )
12
13 func Example() {
14 handler := httpfile.NewWithData("file.json", []byte(`{"hello", "world!"}`))
15
16 w := httptest.NewRecorder()
17 r, _ := http.NewRequest(http.MethodGet, "/", nil)
18 r.Header.Set("Accept-encoding", "identity")
19
20 handler.ServeHTTP(w, r)
21
22 fmt.Println(w.Body)
23
24 handler.ReadFrom(bytes.NewBuffer([]byte(`{"foo": "bar"}`)))
25
26 r.Header.Set("Accept-encoding", "identity")
27
28 w = httptest.NewRecorder()
29
30 handler.ServeHTTP(w, r)
31
32 fmt.Println(w.Body)
33
34 // Output:
35 // {"hello", "world!"}
36 // {"foo": "bar"}
37 }
38