@@ -32,6 +32,7 @@ import (
32
32
"net/http/httptest"
33
33
"net/url"
34
34
"path/filepath"
35
+ "strings"
35
36
"testing"
36
37
"time"
37
38
@@ -2010,52 +2011,142 @@ func TestHeaderGenFuncs(t *testing.T) {
2010
2011
}
2011
2012
2012
2013
func TestGetHealthTestFile (t * testing.T ) {
2013
- router := gin .Default ()
2014
- router .GET ("/api/v1.0/director/healthTest/*path" , getHealthTestFile )
2015
-
2016
- t .Run ("400-on-empty-path" , func (t * testing.T ) {
2017
- w := httptest .NewRecorder ()
2018
- req , _ := http .NewRequest ("GET" , "/api/v1.0/director/healthTest/" , nil )
2019
- router .ServeHTTP (w , req )
2020
-
2021
- assert .Equal (t , http .StatusBadRequest , w .Code )
2022
- })
2023
-
2024
- t .Run ("400-on-random-path" , func (t * testing.T ) {
2025
- w := httptest .NewRecorder ()
2026
- req , _ := http .NewRequest ("GET" , "/api/v1.0/director/healthTest/foo/bar" , nil )
2027
- router .ServeHTTP (w , req )
2028
-
2029
- assert .Equal (t , http .StatusBadRequest , w .Code )
2030
- })
2031
-
2032
- t .Run ("400-on-dir" , func (t * testing.T ) {
2033
- w := httptest .NewRecorder ()
2034
- req , _ := http .NewRequest ("GET" , "/api/v1.0/director/healthTest/pelican/monitoring" , nil )
2035
- router .ServeHTTP (w , req )
2036
-
2037
- assert .Equal (t , http .StatusBadRequest , w .Code )
2038
- })
2039
-
2040
- t .Run ("400-on-missing-file-ext" , func (t * testing.T ) {
2041
- w := httptest .NewRecorder ()
2042
- req , _ := http .NewRequest ("GET" , "/api/v1.0/director/healthTest/pelican/monitoring/testfile" , nil )
2043
- router .ServeHTTP (w , req )
2044
-
2045
- assert .Equal (t , http .StatusBadRequest , w .Code )
2046
- })
2014
+ gEngine := gin .Default ()
2015
+ router := gEngine .Group ("/" )
2016
+ ctx := context .Background ()
2017
+ ctx , cancel , _ := test_utils .TestContext (ctx , t )
2018
+ defer cancel ()
2019
+ RegisterDirectorAPI (ctx , router )
2047
2020
2048
- t .Run ("200-on-correct-request-file" , func (t * testing.T ) {
2049
- w := httptest .NewRecorder ()
2050
- req , _ := http .NewRequest ("GET" , "/api/v1.0/director/healthTest/pelican/monitoring/testfile.txt" , nil )
2051
- router .ServeHTTP (w , req )
2021
+ tests := []struct {
2022
+ name string
2023
+ method string
2024
+ url string
2025
+ wantStatus int
2026
+ wantBody string
2027
+ }{
2028
+ {
2029
+ name : "400-on-empty-path" ,
2030
+ method : "GET" ,
2031
+ url : "/api/v1.0/director/healthTest/" ,
2032
+ wantStatus : http .StatusBadRequest ,
2033
+ },
2034
+ {
2035
+ name : "400-on-random-path" ,
2036
+ method : "GET" ,
2037
+ url : "/api/v1.0/director/healthTest/foo/bar" ,
2038
+ wantStatus : http .StatusBadRequest ,
2039
+ },
2040
+ {
2041
+ name : "400-on-dir" ,
2042
+ method : "GET" ,
2043
+ url : "/api/v1.0/director/healthTest/pelican/monitoring" ,
2044
+ wantStatus : http .StatusBadRequest ,
2045
+ },
2046
+ {
2047
+ name : "400-on-missing-file-ext-self-test" ,
2048
+ method : "GET" ,
2049
+ url : "/api/v1.0/director/healthTest/pelican/monitoring/selfTest/testfile" ,
2050
+ wantStatus : http .StatusBadRequest ,
2051
+ wantBody : "{\" status\" :\" error\" ,\" msg\" :\" Test file name is missing file extension: /pelican/monitoring/selfTest/testfile\" }" ,
2052
+ },
2053
+ {
2054
+ name : "400-on-missing-file-ext-director-test" ,
2055
+ method : "GET" ,
2056
+ url : "/api/v1.0/director/healthTest/pelican/monitoring/directorTest/testfile" ,
2057
+ wantStatus : http .StatusBadRequest ,
2058
+ wantBody : "{\" status\" :\" error\" ,\" msg\" :\" Test file name is missing file extension: /pelican/monitoring/directorTest/testfile\" }" ,
2059
+ },
2060
+ {
2061
+ name : "400-on-bad-timestamp-self-test" ,
2062
+ method : "GET" ,
2063
+ url : "/api/v1.0/director/healthTest/pelican/monitoring/selfTest/self-test-123123123123123.txt" ,
2064
+ wantStatus : http .StatusBadRequest ,
2065
+ wantBody : "{\" status\" :\" error\" ,\" msg\" :\" Invalid timestamp in file name: '123123123123123'. Should conform to 2006-01-02T15:04:05Z07:00 format (RFC 3339)\" }" ,
2066
+ },
2067
+ {
2068
+ name : "400-on-bad-timestamp-director-test" ,
2069
+ method : "GET" ,
2070
+ url : "/api/v1.0/director/healthTest/pelican/monitoring/directorTest/director-test-123123123123123.txt" ,
2071
+ wantStatus : http .StatusBadRequest ,
2072
+ wantBody : "{\" status\" :\" error\" ,\" msg\" :\" Invalid timestamp in file name: '123123123123123'. Should conform to 2006-01-02T15:04:05Z07:00 format (RFC 3339)\" }" ,
2073
+ },
2074
+ {
2075
+ name : "200-on-correct-request-file-self-test" ,
2076
+ method : "GET" ,
2077
+ url : "/api/v1.0/director/healthTest/pelican/monitoring/selfTest/self-test-2006-01-02T15:04:10Z.txt" ,
2078
+ wantStatus : http .StatusOK ,
2079
+ wantBody : server_utils .DirectorTestBody + "\n " ,
2080
+ },
2081
+ {
2082
+ name : "200-on-correct-request-file-director-test" ,
2083
+ method : "GET" ,
2084
+ url : "/api/v1.0/director/healthTest/pelican/monitoring/directorTest/director-test-2006-01-02T15:04:10Z.txt" ,
2085
+ wantStatus : http .StatusOK ,
2086
+ wantBody : server_utils .DirectorTestBody + "\n " ,
2087
+ },
2088
+ {
2089
+ name : "207-and-XML-on-PROPFIND-self-test" ,
2090
+ method : "PROPFIND" ,
2091
+ url : "/api/v1.0/director/healthTest/pelican/monitoring/selfTest/self-test-2006-01-02T15:04:10Z.txt" ,
2092
+ wantStatus : http .StatusMultiStatus ,
2093
+ wantBody : `<?xml version="1.0" encoding="utf-8"?>
2094
+ <D:multistatus xmlns:D="DAV:" xmlns:ns1="http://apache.org/dav/props/" xmlns:ns0="DAV:">
2095
+ <D:response xmlns:lp1="DAV:" xmlns:lp2="http://apache.org/dav/props/" xmlns:lp3="LCGDM:">
2096
+ <D:href>/pelican/monitoring/selfTest/self-test-2006-01-02T15:04:10Z.txt</D:href>
2097
+ <D:propstat>
2098
+ <D:prop>
2099
+ <lp1:getcontentlength>67</lp1:getcontentlength>
2100
+ <lp1:getlastmodified>Mon, 02 Jan 2006 15:04:10 GMT</lp1:getlastmodified>
2101
+ <lp1:iscollection>0</lp1:iscollection>
2102
+ <lp1:executable>F</lp1:executable>
2103
+ </D:prop>
2104
+ <D:status>HTTP/1.1 200 OK</D:status>
2105
+ </D:propstat>
2106
+ </D:response>
2107
+ </D:multistatus>` ,
2108
+ },
2109
+ {
2110
+ name : "207-and-XML-on-PROPFIND-director-test" ,
2111
+ method : "PROPFIND" ,
2112
+ url : "/api/v1.0/director/healthTest/pelican/monitoring/directorTest/director-test-2006-01-02T15:04:10Z.txt" ,
2113
+ wantStatus : http .StatusMultiStatus ,
2114
+ wantBody : `<?xml version="1.0" encoding="utf-8"?>
2115
+ <D:multistatus xmlns:D="DAV:" xmlns:ns1="http://apache.org/dav/props/" xmlns:ns0="DAV:">
2116
+ <D:response xmlns:lp1="DAV:" xmlns:lp2="http://apache.org/dav/props/" xmlns:lp3="LCGDM:">
2117
+ <D:href>/pelican/monitoring/directorTest/director-test-2006-01-02T15:04:10Z.txt</D:href>
2118
+ <D:propstat>
2119
+ <D:prop>
2120
+ <lp1:getcontentlength>67</lp1:getcontentlength>
2121
+ <lp1:getlastmodified>Mon, 02 Jan 2006 15:04:10 GMT</lp1:getlastmodified>
2122
+ <lp1:iscollection>0</lp1:iscollection>
2123
+ <lp1:executable>F</lp1:executable>
2124
+ </D:prop>
2125
+ <D:status>HTTP/1.1 200 OK</D:status>
2126
+ </D:propstat>
2127
+ </D:response>
2128
+ </D:multistatus>` ,
2129
+ },
2130
+ }
2052
2131
2053
- require .Equal (t , http .StatusOK , w .Code )
2132
+ for _ , tt := range tests {
2133
+ t .Run (tt .name , func (t * testing.T ) {
2134
+ w := httptest .NewRecorder ()
2135
+ req , _ := http .NewRequest (tt .method , tt .url , nil )
2136
+ gEngine .ServeHTTP (w , req )
2054
2137
2055
- bytes , err := io .ReadAll (w .Result ().Body )
2056
- require .NoError (t , err )
2057
- assert .Equal (t , server_utils .DirectorTestBody + "\n " , string (bytes ))
2058
- })
2138
+ assert .Equal (t , tt .wantStatus , w .Code )
2139
+ if tt .wantBody != "" {
2140
+ bytes , err := io .ReadAll (w .Result ().Body )
2141
+ require .NoError (t , err )
2142
+ // Normalize whitespace in the response body and expected body so
2143
+ // we can compare them directly
2144
+ actual := strings .Join (strings .Fields (string (bytes )), " " )
2145
+ expected := strings .Join (strings .Fields (tt .wantBody ), " " )
2146
+ assert .Equal (t , expected , actual )
2147
+ }
2148
+ })
2149
+ }
2059
2150
}
2060
2151
2061
2152
func TestHandleFilterServer (t * testing.T ) {
0 commit comments