2022-07-29 10:41:21 +02:00
|
|
|
// Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
|
|
|
|
// All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2022-06-22 05:58:03 +02:00
|
|
|
package lrucache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHandlerBasics(t *testing.T) {
|
|
|
|
r := httptest.NewRequest(http.MethodGet, "/test1", nil)
|
|
|
|
rw := httptest.NewRecorder()
|
|
|
|
shouldBeCalled := true
|
|
|
|
|
|
|
|
handler := NewHttpHandler(1000, time.Second, http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
rw.Write([]byte("Hello World!"))
|
|
|
|
|
|
|
|
if !shouldBeCalled {
|
|
|
|
t.Fatal("fetcher expected to be called")
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
handler.ServeHTTP(rw, r)
|
|
|
|
|
|
|
|
if rw.Code != 200 {
|
|
|
|
t.Fatal("unexpected status code")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(rw.Body.Bytes(), []byte("Hello World!")) {
|
|
|
|
t.Fatal("unexpected body")
|
|
|
|
}
|
|
|
|
|
|
|
|
rw = httptest.NewRecorder()
|
|
|
|
shouldBeCalled = false
|
|
|
|
handler.ServeHTTP(rw, r)
|
|
|
|
|
|
|
|
if rw.Code != 200 {
|
|
|
|
t.Fatal("unexpected status code")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(rw.Body.Bytes(), []byte("Hello World!")) {
|
|
|
|
t.Fatal("unexpected body")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-23 09:25:07 +02:00
|
|
|
// func TestHandlerExpiration(t *testing.T) {
|
|
|
|
// r := httptest.NewRequest(http.MethodGet, "/test1", nil)
|
|
|
|
// rw := httptest.NewRecorder()
|
|
|
|
// i := 1
|
|
|
|
// now := time.Now()
|
2022-06-22 05:58:03 +02:00
|
|
|
|
2022-06-23 09:25:07 +02:00
|
|
|
// handler := NewHttpHandler(1000, 1*time.Second, http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
// rw.Header().Set("Expires", now.Add(10*time.Millisecond).Format(http.TimeFormat))
|
|
|
|
// rw.Write([]byte(strconv.Itoa(i)))
|
|
|
|
// }))
|
2022-06-22 05:58:03 +02:00
|
|
|
|
2022-06-23 09:25:07 +02:00
|
|
|
// handler.ServeHTTP(rw, r)
|
|
|
|
// if !(rw.Body.String() == strconv.Itoa(1)) {
|
|
|
|
// t.Fatal("unexpected body")
|
|
|
|
// }
|
2022-06-22 05:58:03 +02:00
|
|
|
|
2022-06-23 09:25:07 +02:00
|
|
|
// i += 1
|
2022-06-22 05:58:03 +02:00
|
|
|
|
2022-06-23 09:25:07 +02:00
|
|
|
// time.Sleep(11 * time.Millisecond)
|
|
|
|
// rw = httptest.NewRecorder()
|
|
|
|
// handler.ServeHTTP(rw, r)
|
|
|
|
// if !(rw.Body.String() == strconv.Itoa(1)) {
|
|
|
|
// t.Fatal("unexpected body")
|
|
|
|
// }
|
|
|
|
// }
|