Skip to content

Commit

Permalink
Implement provider handler
Browse files Browse the repository at this point in the history
  • Loading branch information
koplas committed Sep 16, 2024
1 parent d488e39 commit 714735d
Showing 1 changed file with 61 additions and 8 deletions.
69 changes: 61 additions & 8 deletions cmd/csaf_downloader/downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,74 @@ package main

import (
"context"
"html/template"
"log/slog"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"

"github.com/csaf-poc/csaf_distribution/v3/internal/options"
"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) {
tests := []struct {
name string
wantSha256 bool
wantSha512 bool
name string
directoryProvider bool
wantSha256 bool
wantSha512 bool
}{
{
name: "want sha256 and sha512",
wantSha256: true,
wantSha512: true,
name: "want sha256 and sha512",
directoryProvider: false,
wantSha256: true,
wantSha512: true,
},
}

Expand All @@ -38,8 +87,12 @@ func TestShaMarking(t *testing.T) {
t.Run(test.name, func(tt *testing.T) {
tt.Parallel()
serverURL := ""
fs := http.FileServer(http.Dir("../../testdata/simple-rolie-provider"))
server := httptest.NewTLSServer(fs)
params := ProviderParams{
url: "",
enableSha256: true,
enableSha512: true,
}
server := httptest.NewTLSServer(ProviderHandler(&params, test.directoryProvider))
defer server.Close()

serverURL = server.URL
Expand Down

0 comments on commit 714735d

Please sign in to comment.