@@ -31,6 +31,7 @@ func TestBasicAuthenticator(t *testing.T) {
3131 })
3232 assert .Equal (t , http .StatusOK , resp .StatusCode )
3333 assert .NoError (t , resp .Body .Close ())
34+ assert .Empty (t , resp .Header .Get ("WWW-Authenticate" ))
3435 })
3536
3637 t .Run ("success_ptr" , func (t * testing.T ) {
@@ -50,6 +51,7 @@ func TestBasicAuthenticator(t *testing.T) {
5051 })
5152 assert .Equal (t , http .StatusOK , resp .StatusCode )
5253 assert .NoError (t , resp .Body .Close ())
54+ assert .Empty (t , resp .Header .Get ("WWW-Authenticate" ))
5355 })
5456
5557 t .Run ("wrong_password" , func (t * testing.T ) {
@@ -69,6 +71,7 @@ func TestBasicAuthenticator(t *testing.T) {
6971 assert .NoError (t , resp .Body .Close ())
7072 require .NoError (t , err )
7173 assert .Equal (t , map [string ]string {"error" : server .Lang .GetDefault ().Get ("auth.invalid-credentials" )}, body )
74+ assert .Equal (t , `Basic realm="Authorization required", charset="UTF-8"` , resp .Header .Get ("WWW-Authenticate" ))
7275 })
7376
7477 t .Run ("service_error" , func (t * testing.T ) {
@@ -87,6 +90,7 @@ func TestBasicAuthenticator(t *testing.T) {
8790 })
8891 assert .Equal (t , http .StatusInternalServerError , resp .StatusCode )
8992 assert .NoError (t , resp .Body .Close ())
93+ assert .Empty (t , resp .Header .Get ("WWW-Authenticate" ))
9094 })
9195
9296 t .Run ("optional_success" , func (t * testing.T ) {
@@ -107,14 +111,15 @@ func TestBasicAuthenticator(t *testing.T) {
107111 })
108112 assert .Equal (t , http .StatusOK , resp .StatusCode )
109113 assert .NoError (t , resp .Body .Close ())
114+ assert .Empty (t , resp .Header .Get ("WWW-Authenticate" ))
110115 })
111116
112117 t .Run ("optional_wrong_password" , func (t * testing.T ) {
113118 server , user := prepareAuthenticatorTest (t )
114119 mockUserService := & MockUserService [TestUser ]{user : user }
115120 a := NewBasicAuthenticator (mockUserService , "Password" )
116121 a .Optional = true
117- authenticator := Middleware ( a )
122+ authenticator := MiddlewareWithRealm ( a , "custom realm" )
118123
119124 request := server .NewTestRequest (http .MethodGet , "/protected" , nil )
120125 request .Request ().SetBasicAuth (user .Email , "wrong password" )
@@ -128,6 +133,7 @@ func TestBasicAuthenticator(t *testing.T) {
128133 assert .NoError (t , resp .Body .Close ())
129134 require .NoError (t , err )
130135 assert .Equal (t , map [string ]string {"error" : server .Lang .GetDefault ().Get ("auth.invalid-credentials" )}, body )
136+ assert .Equal (t , `Basic realm="custom realm", charset="UTF-8"` , resp .Header .Get ("WWW-Authenticate" ))
131137 })
132138
133139 t .Run ("optional_no_auth" , func (t * testing.T ) {
@@ -145,6 +151,7 @@ func TestBasicAuthenticator(t *testing.T) {
145151 })
146152 assert .Equal (t , http .StatusOK , resp .StatusCode )
147153 assert .NoError (t , resp .Body .Close ())
154+ assert .Empty (t , resp .Header .Get ("WWW-Authenticate" ))
148155 })
149156
150157 t .Run ("no_auth" , func (t * testing.T ) {
@@ -164,6 +171,7 @@ func TestBasicAuthenticator(t *testing.T) {
164171 assert .NoError (t , resp .Body .Close ())
165172 require .NoError (t , err )
166173 assert .Equal (t , map [string ]string {"error" : server .Lang .GetDefault ().Get ("auth.no-credentials-provided" )}, body )
174+ assert .Equal (t , `Basic realm="Authorization required", charset="UTF-8"` , resp .Header .Get ("WWW-Authenticate" ))
167175 })
168176
169177 t .Run ("non-existing_password_field" , func (t * testing.T ) {
@@ -182,6 +190,7 @@ func TestBasicAuthenticator(t *testing.T) {
182190 })
183191 assert .Equal (t , http .StatusInternalServerError , resp .StatusCode )
184192 assert .NoError (t , resp .Body .Close ())
193+ assert .Empty (t , resp .Header .Get ("WWW-Authenticate" ))
185194 })
186195}
187196
@@ -200,6 +209,7 @@ func TestConfigBasicAuthenticator(t *testing.T) {
200209 })
201210 assert .Equal (t , http .StatusOK , resp .StatusCode )
202211 assert .NoError (t , resp .Body .Close ())
212+ assert .Empty (t , resp .Header .Get ("WWW-Authenticate" ))
203213 })
204214
205215 t .Run ("wrong_password" , func (t * testing.T ) {
@@ -219,6 +229,7 @@ func TestConfigBasicAuthenticator(t *testing.T) {
219229 assert .NoError (t , resp .Body .Close ())
220230 require .NoError (t , err )
221231 assert .Equal (t , map [string ]string {"error" : server .Lang .GetDefault ().Get ("auth.invalid-credentials" )}, body )
232+ assert .Equal (t , `Basic realm="Authorization required", charset="UTF-8"` , resp .Header .Get ("WWW-Authenticate" ))
222233 })
223234
224235 t .Run ("no_auth" , func (t * testing.T ) {
@@ -228,7 +239,7 @@ func TestConfigBasicAuthenticator(t *testing.T) {
228239 server := testutil .NewTestServerWithOptions (t , goyave.Options {Config : cfg })
229240 request := server .NewTestRequest (http .MethodGet , "/protected" , nil )
230241 request .Route = & goyave.Route {Meta : map [string ]any {MetaAuth : true }}
231- resp := server .TestMiddleware (ConfigBasicAuth ( ), request , func (response * goyave.Response , _ * goyave.Request ) {
242+ resp := server .TestMiddleware (ConfigBasicAuthWithRealm ( "custom realm" ), request , func (response * goyave.Response , _ * goyave.Request ) {
232243 assert .Fail (t , "middleware passed despite failed authentication" )
233244 response .Status (http .StatusOK )
234245 })
@@ -237,5 +248,6 @@ func TestConfigBasicAuthenticator(t *testing.T) {
237248 assert .NoError (t , resp .Body .Close ())
238249 require .NoError (t , err )
239250 assert .Equal (t , map [string ]string {"error" : server .Lang .GetDefault ().Get ("auth.no-credentials-provided" )}, body )
251+ assert .Equal (t , `Basic realm="custom realm", charset="UTF-8"` , resp .Header .Get ("WWW-Authenticate" ))
240252 })
241253}
0 commit comments