Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 0 additions & 32 deletions backend/internal/web/captured/rw_test.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package captured

import (
"cmp"
"fmt"
"math"
"net/http"
"strconv"
)
Expand All @@ -10,6 +12,7 @@ type ResponseWriter struct {
wrapped http.ResponseWriter
statusCode int
written bool
size uint
}

func New(w http.ResponseWriter) *ResponseWriter {
Expand All @@ -22,6 +25,7 @@ func (rw *ResponseWriter) Header() http.Header {

func (rw *ResponseWriter) Write(n []byte) (int, error) {
rw.written = true
rw.size += uint(len(n))
return rw.wrapped.Write(n)
}

Expand All @@ -45,3 +49,20 @@ func (rw ResponseWriter) StatusRepresentation() string {
}
return strconv.Itoa(rw.statusCode)
}

func bytes(i uint) string {
units := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"}
j := 0
for k := i; k >= 1024 && j+1 < len(units); k /= 1024 {
j++
}
d := float64(i) / math.Pow(1024, float64(j))
if d != float64(int(d)) {
return fmt.Sprintf("%.1f%s", d, units[j])
}
return fmt.Sprintf("%d%s", int(d), units[j])
}

func (rw ResponseWriter) SizeRepresentation() string {
return bytes(rw.size)
}
66 changes: 66 additions & 0 deletions backend/internal/web/reception/captured/rw_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package captured

import (
"bufio"
"fmt"
"net"
"net/http"
"net/http/httptest"
"testing"
)

// fake
type hijacker struct {
http.ResponseWriter
}

func (h hijacker) Hijack() (net.Conn, *bufio.ReadWriter, error) {
panic("not to call")
}

var _ http.Hijacker = (*hijacker)(nil)

func ExampleResponseWriter_additionalMethods() {
var rw http.ResponseWriter = New(hijacker{})
if _, ok := rw.(http.Hijacker); !ok {
fmt.Println("the non-ResponseWriter methods are not available directly")
}
if _, ok := rw.(interface{ Unwrap() http.ResponseWriter }).Unwrap().(http.Hijacker); ok {
fmt.Println("become so, after unwrapping")
}
// Output:
// the non-ResponseWriter methods are not available directly
// become so, after unwrapping
}

func TestBytes(t *testing.T) {
tcs := map[uint]string{
0: "0B",
10: "10B",
20: "20B",
1000: "1000B",
1024: "1KB",
1024 + 102: "1.1KB",
1000 * 1024: "1000KB",
1024 * 1024: "1MB",
1.5 * 1024 * 1024: "1.5MB",
1<<63 - 1: "8EB",
}
for input, expected := range tcs {
t.Run(fmt.Sprintf("%d", input), func(t *testing.T) {
got := bytes(input)
if got != expected {
t.Errorf("expected %q got %q", expected, got)
}
})
}
}

func TestResponseWriter_SizeRepresentation(t *testing.T) {
crw := New(httptest.NewRecorder())
crw.Write([]byte("lorem ipsum dolor sit amet consectetur adipscing elit"))
expected, got := "53B", crw.SizeRepresentation()
if expected != got {
t.Errorf("expected %q got %q", expected, got)
}
}
2 changes: 1 addition & 1 deletion backend/internal/web/reception/receptionist.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

"logbook/config/deployment"
"logbook/internal/logger"
"logbook/internal/web/captured"
"logbook/internal/web/reception/captured"
"logbook/internal/web/reception/summarizer"
"logbook/models/columns"
)
Expand Down
2 changes: 1 addition & 1 deletion backend/internal/web/reception/summarizer/summarizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/http"
"time"

"logbook/internal/web/captured"
"logbook/internal/web/reception/captured"
)

type colorizer interface {
Expand Down
Loading