| 
									
										
										
										
											2015-05-11 15:43:54 -07:00
										 |  |  | package middleware | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"encoding/base64" | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2015-07-05 23:21:05 +10:00
										 |  |  | 	"net/http/httptest" | 
					
						
							| 
									
										
										
										
											2015-05-11 15:43:54 -07:00
										 |  |  | 	"testing" | 
					
						
							| 
									
										
										
										
											2015-05-17 22:54:29 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/labstack/echo" | 
					
						
							| 
									
										
										
										
											2015-05-30 10:54:55 -07:00
										 |  |  | 	"github.com/stretchr/testify/assert" | 
					
						
							| 
									
										
										
										
											2015-05-11 15:43:54 -07:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestBasicAuth(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2015-05-30 10:54:55 -07:00
										 |  |  | 	req, _ := http.NewRequest(echo.GET, "/", nil) | 
					
						
							|  |  |  | 	rec := httptest.NewRecorder() | 
					
						
							|  |  |  | 	c := echo.NewContext(req, echo.NewResponse(rec), echo.New()) | 
					
						
							| 
									
										
										
										
											2015-05-11 15:43:54 -07:00
										 |  |  | 	fn := func(u, p string) bool { | 
					
						
							|  |  |  | 		if u == "joe" && p == "secret" { | 
					
						
							|  |  |  | 			return true | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-05-13 23:07:03 -07:00
										 |  |  | 	ba := BasicAuth(fn) | 
					
						
							| 
									
										
										
										
											2015-05-11 15:43:54 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Valid credentials | 
					
						
							|  |  |  | 	auth := Basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret")) | 
					
						
							|  |  |  | 	req.Header.Set(echo.Authorization, auth) | 
					
						
							| 
									
										
										
										
											2015-05-30 10:54:55 -07:00
										 |  |  | 	assert.NoError(t, ba(c)) | 
					
						
							| 
									
										
										
										
											2015-05-11 15:43:54 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	//--------------------- | 
					
						
							|  |  |  | 	// Invalid credentials | 
					
						
							|  |  |  | 	//--------------------- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-16 12:49:01 -07:00
										 |  |  | 	// Incorrect password | 
					
						
							| 
									
										
										
										
											2015-06-09 20:06:51 -07:00
										 |  |  | 	auth = Basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:password")) | 
					
						
							| 
									
										
										
										
											2015-05-11 15:43:54 -07:00
										 |  |  | 	req.Header.Set(echo.Authorization, auth) | 
					
						
							| 
									
										
										
										
											2015-05-21 14:02:29 -07:00
										 |  |  | 	he := ba(c).(*echo.HTTPError) | 
					
						
							| 
									
										
										
										
											2015-05-30 10:54:55 -07:00
										 |  |  | 	assert.Equal(t, http.StatusUnauthorized, he.Code()) | 
					
						
							| 
									
										
										
										
											2015-09-01 11:24:36 -07:00
										 |  |  | 	assert.Equal(t, Basic + " realm=Restricted", rec.Header().Get(echo.WWWAuthenticate)) | 
					
						
							| 
									
										
										
										
											2015-05-17 22:54:29 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Empty Authorization header | 
					
						
							|  |  |  | 	req.Header.Set(echo.Authorization, "") | 
					
						
							| 
									
										
										
										
											2015-05-21 14:02:29 -07:00
										 |  |  | 	he = ba(c).(*echo.HTTPError) | 
					
						
							| 
									
										
										
										
											2015-09-01 11:24:36 -07:00
										 |  |  | 	assert.Equal(t, http.StatusUnauthorized, he.Code()) | 
					
						
							|  |  |  | 	assert.Equal(t, Basic + " realm=Restricted", rec.Header().Get(echo.WWWAuthenticate)) | 
					
						
							| 
									
										
										
										
											2015-05-11 15:43:54 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-17 22:54:29 -07:00
										 |  |  | 	// Invalid Authorization header | 
					
						
							| 
									
										
										
										
											2015-07-05 23:21:05 +10:00
										 |  |  | 	auth = base64.StdEncoding.EncodeToString([]byte("invalid")) | 
					
						
							| 
									
										
										
										
											2015-05-11 15:43:54 -07:00
										 |  |  | 	req.Header.Set(echo.Authorization, auth) | 
					
						
							| 
									
										
										
										
											2015-05-21 14:02:29 -07:00
										 |  |  | 	he = ba(c).(*echo.HTTPError) | 
					
						
							| 
									
										
										
										
											2015-09-01 11:24:36 -07:00
										 |  |  | 	assert.Equal(t, http.StatusUnauthorized, he.Code()) | 
					
						
							|  |  |  | 	assert.Equal(t, Basic + " realm=Restricted", rec.Header().Get(echo.WWWAuthenticate)) | 
					
						
							| 
									
										
										
										
											2015-05-30 10:54:55 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// WebSocket | 
					
						
							|  |  |  | 	c.Request().Header.Set(echo.Upgrade, echo.WebSocket) | 
					
						
							|  |  |  | 	assert.NoError(t, ba(c)) | 
					
						
							| 
									
										
										
										
											2015-05-11 15:43:54 -07:00
										 |  |  | } |