-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added Authorization to HeaderBuilder. - New constructor for Chain type. - Changed GetReader/SetWriter methods to Read/Write (Header). - Improved SessionStore testing to use SalterFast to more reliable timing. - New testing for BasicAuthenticator and Chain types.
- Loading branch information
Showing
10 changed files
with
205 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright 2016 Fabrício Godoy | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package web | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
type FooAuthenticator int | ||
|
||
func (a *FooAuthenticator) TryAuthentication( | ||
r *http.Request, | ||
user, secret string, | ||
) bool { | ||
if user == "user" && secret == "secret" { | ||
*a = FooAuthenticator(int(*a) + 1) | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func (a *FooAuthenticator) EndPoint(w http.ResponseWriter, r *http.Request) { | ||
*a = FooAuthenticator(int(*a) * 10) | ||
} | ||
|
||
func TestBasicAuthenticator(t *testing.T) { | ||
testValues := []struct { | ||
user string | ||
secret string | ||
}{ | ||
{"user", "user"}, | ||
{"user", "secret"}, | ||
{"user", "123"}, | ||
{"user", ""}, | ||
{"secret", "user"}, | ||
{"secret", "secret"}, | ||
{"secret", "123"}, | ||
{"secret", ""}, | ||
{"123", "user"}, | ||
{"123", "secret"}, | ||
{"123", "123"}, | ||
{"123", ""}, | ||
{"", "user"}, | ||
{"", "secret"}, | ||
{"", "123"}, | ||
{"", ""}, | ||
} | ||
|
||
for idx, testVal := range testValues { | ||
foo := FooAuthenticator(1) | ||
basicauth := BasicAuthenticator{&foo} | ||
|
||
chain := NewChain() | ||
chain = append(chain, basicauth.AuthHandler) | ||
server := chain.Get(http.HandlerFunc(foo.EndPoint)) | ||
|
||
w := httptest.NewRecorder() | ||
req, _ := http.NewRequest("GET", "http://localhost", nil) | ||
NewHeader(). | ||
Authorization(testVal.user, testVal.secret). | ||
Write(req.Header) | ||
|
||
server.ServeHTTP(w, req) | ||
|
||
switch int(foo) { | ||
case 1: | ||
if idx == 1 { | ||
t.Error("Failed authentication: neither the middleware and endpoint was called") | ||
} | ||
case 2: | ||
t.Error("Failed authentication: endpoint was not called") | ||
case 10: | ||
t.Error("Failed authentication: enpoint was called without authentication") | ||
case 20: | ||
if idx != 1 { | ||
t.Errorf("Should not authenticate") | ||
} | ||
default: | ||
t.Errorf("Failed authentication: unexpected value %d", int(foo)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
BenchmarkSessionCreation-4 2000 781448 ns/op 12961 B/op 201 allocs/op | ||
BenchmarkSessionCreationFast-4 100000 14922 ns/op 1077 B/op 15 allocs/op | ||
BenchmarkSessionCreation-4 2000 774228 ns/op 10982 B/op 170 allocs/op | ||
BenchmarkSessionCreationFast-4 100000 14867 ns/op 1009 B/op 14 allocs/op |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2016 Fabrício Godoy | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package web | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
var stacker []int | ||
|
||
func TestChainOrder(t *testing.T) { | ||
stacker = make([]int, 0) | ||
|
||
chain := NewChain() | ||
chain = append(chain, FooHandler(1).Middleware) | ||
chain = append(chain, FooHandler(2).Middleware) | ||
chain = append(chain, FooHandler(3).Middleware) | ||
chain = append(chain, FooHandler(4).Middleware) | ||
chain = append(chain, FooHandler(5).Middleware) | ||
handler := chain.Get(http.HandlerFunc(FooHandler(6).EndPoint)) | ||
handler.ServeHTTP(nil, nil) | ||
|
||
if len(stacker) != 6 { | ||
t.Errorf("Not all handlers was called: %d instead of %d", | ||
len(stacker), 6) | ||
} | ||
|
||
counter := 1 | ||
for _, v := range stacker { | ||
if v != counter { | ||
t.Errorf("Chain not called in order: got %d instead of %d", | ||
v, counter) | ||
} | ||
|
||
counter++ | ||
} | ||
} | ||
|
||
type FooHandler int | ||
|
||
func (h FooHandler) Middleware(next http.Handler) http.Handler { | ||
fn := func(w http.ResponseWriter, r *http.Request) { | ||
stacker = append(stacker, int(h)) | ||
next.ServeHTTP(w, r) | ||
} | ||
|
||
return http.HandlerFunc(fn) | ||
} | ||
|
||
func (h FooHandler) EndPoint(w http.ResponseWriter, r *http.Request) { | ||
stacker = append(stacker, int(h)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters