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
79 changes: 79 additions & 0 deletions internal/catalogd/server/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import (
"bytes"
"context"
"encoding/json"
"io"
"io/fs"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
"time"

"github.com/graphql-go/graphql"
"go.uber.org/mock/gomock"
Expand Down Expand Up @@ -263,3 +267,78 @@ func TestAllowedMethodsHandler_POSTOnlyForGraphQL(t *testing.T) {
t.Error("POST should be allowed for GraphQL endpoint at router level")
}
}

func TestHandleV1Metas_IfModifiedSince(t *testing.T) {
const catalogContent = `{"schema":"olm.package","name":"foo"}` + "\n"
modTime := time.Date(2025, time.January, 2, 3, 4, 5, 0, time.UTC)

for _, tc := range []struct {
name string
ifModifiedSince string
expectedCode int
expectedBody string
}{
{
name: "not modified since the given time",
ifModifiedSince: modTime.Format(http.TimeFormat),
expectedCode: http.StatusNotModified,
expectedBody: "",
},
{
name: "modified since the given time",
ifModifiedSince: modTime.Add(-time.Hour).Format(http.TimeFormat),
expectedCode: http.StatusOK,
expectedBody: catalogContent,
},
{
// RFC 9110 section 13.1.3: a recipient MUST ignore the
// If-Modified-Since header field if the field value is not a
// valid HTTP-date.
name: "invalid date is ignored",
ifModifiedSince: "not-a-date",
expectedCode: http.StatusOK,
expectedBody: catalogContent,
},
} {
t.Run(tc.name, func(t *testing.T) {
catalogFile, err := os.Create(filepath.Join(t.TempDir(), "catalog.jsonl"))
if err != nil {
t.Fatal(err)
}
if _, err := catalogFile.WriteString(catalogContent); err != nil {
t.Fatal(err)
}
if _, err := catalogFile.Seek(0, io.SeekStart); err != nil {
t.Fatal(err)
}
if err := os.Chtimes(catalogFile.Name(), modTime, modTime); err != nil {
t.Fatal(err)
}
catalogStat, err := os.Stat(catalogFile.Name())
if err != nil {
t.Fatal(err)
}

ctrl := gomock.NewController(t)
rootURL, _ := url.Parse("http://localhost/")
store := mockcatalogdserver.NewMockCatalogStore(ctrl)
store.EXPECT().GetCatalogData("test-catalog").Return(catalogFile, catalogStat, nil)

handlers := server.NewCatalogHandlers(store, nil, rootURL, server.MetasHandlerEnabled, server.GraphQLQueriesDisabled)
handler := handlers.Handler()

req := httptest.NewRequest(http.MethodGet, "/test-catalog/api/v1/metas", nil)
req.Header.Set("If-Modified-Since", tc.ifModifiedSince)
w := httptest.NewRecorder()

handler.ServeHTTP(w, req)

if w.Code != tc.expectedCode {
t.Errorf("Expected status %d, got %d", tc.expectedCode, w.Code)
}
if w.Body.String() != tc.expectedBody {
t.Errorf("Expected body %q, got %q", tc.expectedBody, w.Body.String())
}
})
}
}
6 changes: 3 additions & 3 deletions internal/catalogd/server/http_preconditions_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,22 @@ func checkPreconditions(w http.ResponseWriter, r *http.Request, modtime time.Tim
return true
}
case condNone:
if checkIfModifiedSince(r, w, modtime) == condFalse {
if checkIfModifiedSince(r, modtime) == condFalse {
writeNotModified(w)
return true
}
}
return false
}

func checkIfModifiedSince(r *http.Request, w http.ResponseWriter, modtime time.Time) condResult {
func checkIfModifiedSince(r *http.Request, modtime time.Time) condResult {
ims := r.Header.Get("If-Modified-Since")
if ims == "" || isZeroTime(modtime) {
return condTrue
}
t, err := parseTime(ims)
if err != nil {
httpError(w, err)
// RFC 9110 section 13.1.3: an invalid HTTP-date must be ignored.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behavior now mirrors the checkIfUnmodifiedSince() behavior.

return condNone
}
// The Last-Modified header truncates sub-second precision so
Expand Down
Loading