-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
1,267 additions
and
102 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package main | ||
|
||
import "testing" | ||
|
||
func TestRun(t *testing.T) { | ||
err := run() | ||
if err != nil { | ||
t.Error("failed run()") | ||
} | ||
} |
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,33 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestNoSurf(t *testing.T) { | ||
var myH myHandler | ||
|
||
h := NoSurf(&myH) | ||
|
||
switch v := h.(type) { | ||
case http.Handler: | ||
// do nothing | ||
default: | ||
t.Error(fmt.Sprintf("type is not http.Handler, but is %T", v)) | ||
} | ||
} | ||
|
||
func TestSessionLoad(t *testing.T) { | ||
var myH myHandler | ||
|
||
h := SessionLoad(&myH) | ||
|
||
switch v := h.(type) { | ||
case http.Handler: | ||
// do nothing | ||
default: | ||
t.Error(fmt.Sprintf("type is not http.Handler, but is %T", v)) | ||
} | ||
} |
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,22 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/abneed/bookings/internal/config" | ||
"github.com/go-chi/chi" | ||
) | ||
|
||
func TestRoutes(t *testing.T) { | ||
var app config.AppConfig | ||
|
||
mux := routes(&app) | ||
|
||
switch v := mux.(type) { | ||
case *chi.Mux: | ||
// do nothing; test passed | ||
default: | ||
t.Error(fmt.Sprintf("type is not *chi.Mux, type is %T", v)) | ||
} | ||
} |
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,18 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
|
||
os.Exit(m.Run()) | ||
} | ||
|
||
type myHandler struct{} | ||
|
||
func (mh *myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
|
||
} |
File renamed without changes.
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,18 @@ | ||
package forms | ||
|
||
type errors map[string][]string | ||
|
||
// Add adds an error message for a given form field | ||
func (e errors) Add(field, message string) { | ||
e[field] = append(e[field], message) | ||
} | ||
|
||
// Get returns the first error message | ||
func (e errors) Get(field string) string { | ||
es := e[field] | ||
if len(es) == 0 { | ||
return "" | ||
} | ||
|
||
return es[0] | ||
} |
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,66 @@ | ||
package forms | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/asaskevich/govalidator" | ||
) | ||
|
||
// Form creates a custom form struct, embeds a url.Values object | ||
type Form struct { | ||
url.Values | ||
Errors errors | ||
} | ||
|
||
// Valid returns true if there are no errors, otherwise false | ||
func (f *Form) Valid() bool { | ||
return len(f.Errors) == 0 | ||
} | ||
|
||
// New initializes a form struct | ||
func New(data url.Values) *Form { | ||
return &Form{ | ||
data, | ||
errors(map[string][]string{}), | ||
} | ||
} | ||
|
||
// Required checks for required fields | ||
func (f *Form) Required(fields ...string) { | ||
for _, field := range fields { | ||
value := f.Get(field) | ||
if strings.TrimSpace(value) == "" { | ||
f.Errors.Add(field, "This field cannot be blank") | ||
} | ||
} | ||
} | ||
|
||
// Has checks if form field is in post and not empty | ||
func (f *Form) Has(field string, r *http.Request) bool { | ||
x := r.Form.Get(field) | ||
if x == "" { | ||
|
||
return false | ||
} | ||
return true | ||
} | ||
|
||
// MinLength checks for strign minimum length | ||
func (f *Form) MinLength(field string, length int, r *http.Request) bool { | ||
x := r.Form.Get(field) | ||
if len(x) < length { | ||
f.Errors.Add(field, fmt.Sprintf("This field must be at least %d characters long", length)) | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
// IsEmail checks for valid email address | ||
func (f *Form) IsEmail(field string) { | ||
if !govalidator.IsEmail(f.Get(field)) { | ||
f.Errors.Add(field, "Invalid email address") | ||
} | ||
} |
Oops, something went wrong.