| 
									
										
										
										
											2016-04-27 21:08:06 -07:00
										 |  |  | package middleware | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"encoding/base64" | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2016-09-22 22:53:44 -07:00
										 |  |  | 	"net/http/httptest" | 
					
						
							| 
									
										
										
										
											2016-04-27 21:08:06 -07:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/labstack/echo" | 
					
						
							|  |  |  | 	"github.com/stretchr/testify/assert" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestBasicAuth(t *testing.T) { | 
					
						
							|  |  |  | 	e := echo.New() | 
					
						
							| 
									
										
										
										
											2016-09-22 22:53:44 -07:00
										 |  |  | 	req, _ := http.NewRequest(echo.GET, "/", nil) | 
					
						
							|  |  |  | 	res := httptest.NewRecorder() | 
					
						
							| 
									
										
										
										
											2016-04-27 21:08:06 -07:00
										 |  |  | 	c := e.NewContext(req, res) | 
					
						
							|  |  |  | 	f := func(u, p string) bool { | 
					
						
							|  |  |  | 		if u == "joe" && p == "secret" { | 
					
						
							|  |  |  | 			return true | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	h := BasicAuth(f)(func(c echo.Context) error { | 
					
						
							|  |  |  | 		return c.String(http.StatusOK, "test") | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Valid credentials | 
					
						
							|  |  |  | 	auth := basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret")) | 
					
						
							| 
									
										
										
										
											2016-09-22 22:53:44 -07:00
										 |  |  | 	req.Header.Set(echo.HeaderAuthorization, auth) | 
					
						
							| 
									
										
										
										
											2016-04-27 21:08:06 -07:00
										 |  |  | 	assert.NoError(t, h(c)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Incorrect password | 
					
						
							|  |  |  | 	auth = basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:password")) | 
					
						
							| 
									
										
										
										
											2016-09-22 22:53:44 -07:00
										 |  |  | 	req.Header.Set(echo.HeaderAuthorization, auth) | 
					
						
							| 
									
										
										
										
											2016-04-27 21:08:06 -07:00
										 |  |  | 	he := h(c).(*echo.HTTPError) | 
					
						
							|  |  |  | 	assert.Equal(t, http.StatusUnauthorized, he.Code) | 
					
						
							|  |  |  | 	assert.Equal(t, basic+" realm=Restricted", res.Header().Get(echo.HeaderWWWAuthenticate)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Empty Authorization header | 
					
						
							| 
									
										
										
										
											2016-09-22 22:53:44 -07:00
										 |  |  | 	req.Header.Set(echo.HeaderAuthorization, "") | 
					
						
							| 
									
										
										
										
											2016-04-27 21:08:06 -07:00
										 |  |  | 	he = h(c).(*echo.HTTPError) | 
					
						
							| 
									
										
										
										
											2016-04-28 07:09:33 -07:00
										 |  |  | 	assert.Equal(t, http.StatusUnauthorized, he.Code) | 
					
						
							| 
									
										
										
										
											2016-04-27 21:08:06 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Invalid Authorization header | 
					
						
							|  |  |  | 	auth = base64.StdEncoding.EncodeToString([]byte("invalid")) | 
					
						
							| 
									
										
										
										
											2016-09-22 22:53:44 -07:00
										 |  |  | 	req.Header.Set(echo.HeaderAuthorization, auth) | 
					
						
							| 
									
										
										
										
											2016-04-27 21:08:06 -07:00
										 |  |  | 	he = h(c).(*echo.HTTPError) | 
					
						
							| 
									
										
										
										
											2016-04-28 07:09:33 -07:00
										 |  |  | 	assert.Equal(t, http.StatusUnauthorized, he.Code) | 
					
						
							| 
									
										
										
										
											2016-04-27 21:08:06 -07:00
										 |  |  | } |