@@ -10,25 +10,74 @@ package main
10
10
11
11
import (
12
12
"context"
13
+ "html/template"
13
14
"log/slog"
14
15
"net/http"
15
16
"net/http/httptest"
17
+ "os"
18
+ "strings"
16
19
"testing"
17
20
18
21
"github.com/csaf-poc/csaf_distribution/v3/internal/options"
19
22
"github.com/csaf-poc/csaf_distribution/v3/util"
20
23
)
21
24
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
+
22
69
func TestShaMarking (t * testing.T ) {
23
70
tests := []struct {
24
- name string
25
- wantSha256 bool
26
- wantSha512 bool
71
+ name string
72
+ directoryProvider bool
73
+ wantSha256 bool
74
+ wantSha512 bool
27
75
}{
28
76
{
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 ,
32
81
},
33
82
}
34
83
@@ -38,8 +87,12 @@ func TestShaMarking(t *testing.T) {
38
87
t .Run (test .name , func (tt * testing.T ) {
39
88
tt .Parallel ()
40
89
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 ))
43
96
defer server .Close ()
44
97
45
98
serverURL = server .URL
0 commit comments