Skip to content

Commit 714735d

Browse files
committed
Implement provider handler
1 parent d488e39 commit 714735d

File tree

1 file changed

+61
-8
lines changed

1 file changed

+61
-8
lines changed

cmd/csaf_downloader/downloader_test.go

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,74 @@ package main
1010

1111
import (
1212
"context"
13+
"html/template"
1314
"log/slog"
1415
"net/http"
1516
"net/http/httptest"
17+
"os"
18+
"strings"
1619
"testing"
1720

1821
"github.com/csaf-poc/csaf_distribution/v3/internal/options"
1922
"github.com/csaf-poc/csaf_distribution/v3/util"
2023
)
2124

25+
type ProviderParams struct {
26+
url string
27+
enableSha256 bool
28+
enableSha512 bool
29+
}
30+
31+
func ProviderHandler(params *ProviderParams, directoryProvider bool) http.HandlerFunc {
32+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
33+
path := "../../testdata/"
34+
if directoryProvider {
35+
path += "simple-directory-provider"
36+
} else {
37+
path += "simple-rolie-provider"
38+
}
39+
40+
path += r.URL.Path
41+
42+
if strings.HasSuffix(r.URL.Path, "/") {
43+
path += "index.html"
44+
}
45+
46+
content, err := os.ReadFile(path)
47+
if err != nil {
48+
w.WriteHeader(http.StatusNotFound)
49+
return
50+
}
51+
switch {
52+
case strings.HasSuffix(path, ".html"):
53+
w.Header().Add("Content-Type", "text/html")
54+
case strings.HasSuffix(path, ".json"):
55+
w.Header().Add("Content-Type", "application/json")
56+
default:
57+
w.Header().Add("Content-Type", "text/plain")
58+
}
59+
60+
tmplt, err := template.New("base").Parse(string(content))
61+
if err != nil {
62+
w.WriteHeader(http.StatusInternalServerError)
63+
return
64+
}
65+
tmplt.Execute(w, params)
66+
})
67+
}
68+
2269
func TestShaMarking(t *testing.T) {
2370
tests := []struct {
24-
name string
25-
wantSha256 bool
26-
wantSha512 bool
71+
name string
72+
directoryProvider bool
73+
wantSha256 bool
74+
wantSha512 bool
2775
}{
2876
{
29-
name: "want sha256 and sha512",
30-
wantSha256: true,
31-
wantSha512: true,
77+
name: "want sha256 and sha512",
78+
directoryProvider: false,
79+
wantSha256: true,
80+
wantSha512: true,
3281
},
3382
}
3483

@@ -38,8 +87,12 @@ func TestShaMarking(t *testing.T) {
3887
t.Run(test.name, func(tt *testing.T) {
3988
tt.Parallel()
4089
serverURL := ""
41-
fs := http.FileServer(http.Dir("../../testdata/simple-rolie-provider"))
42-
server := httptest.NewTLSServer(fs)
90+
params := ProviderParams{
91+
url: "",
92+
enableSha256: true,
93+
enableSha512: true,
94+
}
95+
server := httptest.NewTLSServer(ProviderHandler(&params, test.directoryProvider))
4396
defer server.Close()
4497

4598
serverURL = server.URL

0 commit comments

Comments
 (0)