1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 11:55:40 +01:00

Implement provider handler

This commit is contained in:
koplas 2024-09-16 20:08:21 +02:00
parent d488e39947
commit 714735d74a

View file

@ -10,23 +10,72 @@ package main
import ( import (
"context" "context"
"html/template"
"log/slog" "log/slog"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os"
"strings"
"testing" "testing"
"github.com/csaf-poc/csaf_distribution/v3/internal/options" "github.com/csaf-poc/csaf_distribution/v3/internal/options"
"github.com/csaf-poc/csaf_distribution/v3/util" "github.com/csaf-poc/csaf_distribution/v3/util"
) )
type ProviderParams struct {
url string
enableSha256 bool
enableSha512 bool
}
func ProviderHandler(params *ProviderParams, directoryProvider bool) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path := "../../testdata/"
if directoryProvider {
path += "simple-directory-provider"
} else {
path += "simple-rolie-provider"
}
path += r.URL.Path
if strings.HasSuffix(r.URL.Path, "/") {
path += "index.html"
}
content, err := os.ReadFile(path)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
switch {
case strings.HasSuffix(path, ".html"):
w.Header().Add("Content-Type", "text/html")
case strings.HasSuffix(path, ".json"):
w.Header().Add("Content-Type", "application/json")
default:
w.Header().Add("Content-Type", "text/plain")
}
tmplt, err := template.New("base").Parse(string(content))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
tmplt.Execute(w, params)
})
}
func TestShaMarking(t *testing.T) { func TestShaMarking(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
directoryProvider bool
wantSha256 bool wantSha256 bool
wantSha512 bool wantSha512 bool
}{ }{
{ {
name: "want sha256 and sha512", name: "want sha256 and sha512",
directoryProvider: false,
wantSha256: true, wantSha256: true,
wantSha512: true, wantSha512: true,
}, },
@ -38,8 +87,12 @@ func TestShaMarking(t *testing.T) {
t.Run(test.name, func(tt *testing.T) { t.Run(test.name, func(tt *testing.T) {
tt.Parallel() tt.Parallel()
serverURL := "" serverURL := ""
fs := http.FileServer(http.Dir("../../testdata/simple-rolie-provider")) params := ProviderParams{
server := httptest.NewTLSServer(fs) url: "",
enableSha256: true,
enableSha512: true,
}
server := httptest.NewTLSServer(ProviderHandler(&params, test.directoryProvider))
defer server.Close() defer server.Close()
serverURL = server.URL serverURL = server.URL