From 506c89420a8479b7399003e0f810e2932979def2 Mon Sep 17 00:00:00 2001 From: Kavya Date: Mon, 3 Dec 2018 11:38:42 -0600 Subject: [PATCH 1/2] changes --- exercise-003-web/exhibit-d/home.html | 12 ++++++++++++ exercise-003-web/exhibit-d/server.go | 12 ++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/exercise-003-web/exhibit-d/home.html b/exercise-003-web/exhibit-d/home.html index d00faeb..0c0daef 100644 --- a/exercise-003-web/exhibit-d/home.html +++ b/exercise-003-web/exhibit-d/home.html @@ -7,5 +7,17 @@ + + + + + + {{range $name, $attempts := .}} + + + + + {{end}} +
UserAttempts
{{$name}}{{$attempts}}
diff --git a/exercise-003-web/exhibit-d/server.go b/exercise-003-web/exhibit-d/server.go index d00633c..0ac802c 100644 --- a/exercise-003-web/exhibit-d/server.go +++ b/exercise-003-web/exhibit-d/server.go @@ -1,10 +1,11 @@ package main import ( - "fmt" "html/template" "net/http" + "fmt" ) +var loginInfo = make(map[string]int) var homeT = template.Must(template.ParseFiles("exhibit-d/home.html")) @@ -14,9 +15,12 @@ func home(w http.ResponseWriter, r *http.Request) { func signup(w http.ResponseWriter, r *http.Request) { r.ParseForm() - username := r.Form.Get("username") - msg := "Hey " + username + ", did you try to sign-up?" - fmt.Fprintf(w, msg) + username := r.Form.Get("username") + + loginInfo[username]++ + // username := r.Form.Get("username") + homeT.Execute(w, loginInfo) + } func main() { From 60c4eb934466b0ea852fe92689005925a8dc2769 Mon Sep 17 00:00:00 2001 From: Kavya Date: Mon, 3 Dec 2018 16:35:36 -0600 Subject: [PATCH 2/2] web application --- exercise-003-web/.idea/encodings.xml | 4 + exercise-003-web/.idea/exercise-003-web.iml | 8 + exercise-003-web/.idea/misc.xml | 6 + exercise-003-web/.idea/modules.xml | 8 + exercise-003-web/.idea/vcs.xml | 6 + exercise-003-web/.idea/workspace.xml | 366 ++++++++++++++++++++ exercise-003-web/build.sh | 3 + exercise-003-web/exhibit-c/server.go | 1 + exercise-003-web/exhibit-d/home.html | 28 +- exercise-003-web/exhibit-d/server.go | 12 +- exercise-003-web/exhibit-e/server_test.go | 2 +- exercise-003-web/exhibit-f/home.html | 23 ++ exercise-003-web/exhibit-f/server.go | 47 +++ exercise-003-web/exhibit-f/server_test.go | 65 ++++ 14 files changed, 550 insertions(+), 29 deletions(-) create mode 100644 exercise-003-web/.idea/encodings.xml create mode 100644 exercise-003-web/.idea/exercise-003-web.iml create mode 100644 exercise-003-web/.idea/misc.xml create mode 100644 exercise-003-web/.idea/modules.xml create mode 100644 exercise-003-web/.idea/vcs.xml create mode 100644 exercise-003-web/.idea/workspace.xml create mode 100755 exercise-003-web/build.sh create mode 100644 exercise-003-web/exhibit-f/home.html create mode 100644 exercise-003-web/exhibit-f/server.go create mode 100644 exercise-003-web/exhibit-f/server_test.go diff --git a/exercise-003-web/.idea/encodings.xml b/exercise-003-web/.idea/encodings.xml new file mode 100644 index 0000000..15a15b2 --- /dev/null +++ b/exercise-003-web/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/exercise-003-web/.idea/exercise-003-web.iml b/exercise-003-web/.idea/exercise-003-web.iml new file mode 100644 index 0000000..c956989 --- /dev/null +++ b/exercise-003-web/.idea/exercise-003-web.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/exercise-003-web/.idea/misc.xml b/exercise-003-web/.idea/misc.xml new file mode 100644 index 0000000..28a804d --- /dev/null +++ b/exercise-003-web/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/exercise-003-web/.idea/modules.xml b/exercise-003-web/.idea/modules.xml new file mode 100644 index 0000000..af27631 --- /dev/null +++ b/exercise-003-web/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/exercise-003-web/.idea/vcs.xml b/exercise-003-web/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/exercise-003-web/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/exercise-003-web/.idea/workspace.xml b/exercise-003-web/.idea/workspace.xml new file mode 100644 index 0000000..2cb0428 --- /dev/null +++ b/exercise-003-web/.idea/workspace.xml @@ -0,0 +1,366 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/exercise-003-web/build.sh b/exercise-003-web/build.sh new file mode 100755 index 0000000..312a08a --- /dev/null +++ b/exercise-003-web/build.sh @@ -0,0 +1,3 @@ +gofmt -w . +golint ./... +go run exhibit-f/server.go \ No newline at end of file diff --git a/exercise-003-web/exhibit-c/server.go b/exercise-003-web/exhibit-c/server.go index 81297a8..f533bf2 100644 --- a/exercise-003-web/exhibit-c/server.go +++ b/exercise-003-web/exhibit-c/server.go @@ -5,6 +5,7 @@ import ( "net/http" ) +// View structure type View struct { Name string Age int diff --git a/exercise-003-web/exhibit-d/home.html b/exercise-003-web/exhibit-d/home.html index 0c0daef..778a347 100644 --- a/exercise-003-web/exhibit-d/home.html +++ b/exercise-003-web/exhibit-d/home.html @@ -1,23 +1,11 @@ - Learning Go +Learning Go - -
- - -
- - - - - - {{range $name, $attempts := .}} - - - - - {{end}} -
UserAttempts
{{$name}}{{$attempts}}
- - + +
+ + +
+ + \ No newline at end of file diff --git a/exercise-003-web/exhibit-d/server.go b/exercise-003-web/exhibit-d/server.go index 0ac802c..d00633c 100644 --- a/exercise-003-web/exhibit-d/server.go +++ b/exercise-003-web/exhibit-d/server.go @@ -1,11 +1,10 @@ package main import ( + "fmt" "html/template" "net/http" - "fmt" ) -var loginInfo = make(map[string]int) var homeT = template.Must(template.ParseFiles("exhibit-d/home.html")) @@ -15,12 +14,9 @@ func home(w http.ResponseWriter, r *http.Request) { func signup(w http.ResponseWriter, r *http.Request) { r.ParseForm() - username := r.Form.Get("username") - - loginInfo[username]++ - // username := r.Form.Get("username") - homeT.Execute(w, loginInfo) - + username := r.Form.Get("username") + msg := "Hey " + username + ", did you try to sign-up?" + fmt.Fprintf(w, msg) } func main() { diff --git a/exercise-003-web/exhibit-e/server_test.go b/exercise-003-web/exhibit-e/server_test.go index b53fd6a..8265ac4 100644 --- a/exercise-003-web/exhibit-e/server_test.go +++ b/exercise-003-web/exhibit-e/server_test.go @@ -1,10 +1,10 @@ package main import ( + "github.com/stretchr/testify/assert" "net/http" "net/http/httptest" "testing" - "github.com/stretchr/testify/assert" ) func init() { diff --git a/exercise-003-web/exhibit-f/home.html b/exercise-003-web/exhibit-f/home.html new file mode 100644 index 0000000..c65fa66 --- /dev/null +++ b/exercise-003-web/exhibit-f/home.html @@ -0,0 +1,23 @@ + + + Learning Go + + +
+ + +
+ + + + + + {{range $key, $value := .}} + + + + + {{end}} +
UsernameAttempts
{{$key}}{{$value}}
+ + diff --git a/exercise-003-web/exhibit-f/server.go b/exercise-003-web/exhibit-f/server.go new file mode 100644 index 0000000..a08b3d7 --- /dev/null +++ b/exercise-003-web/exhibit-f/server.go @@ -0,0 +1,47 @@ +package main + +import ( + "fmt" + "html/template" + "log" + "net/http" +) + +var loginInfo = make(map[string]int) + +var homeT *template.Template + +func setup(dir string) { + homeT = template.Must(template.ParseFiles(dir + "/exhibit-f/home.html")) +} + +func home(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case "POST": + r.ParseForm() + username := r.Form.Get("username") + if username != "" { + loginInfo[username]++ + } + err := homeT.Execute(w, loginInfo) + if err != nil { + log.Println(err) + } + case "GET": + err := homeT.Execute(w, loginInfo) + if err != nil { + log.Println(err) + } + default: + fmt.Fprint(w, "Invalid HTTP method") + } +} + +func main() { + setup(".") + http.HandleFunc("/home", home) + err := http.ListenAndServe(":8080", nil) + if err != nil { + panic(err) + } +} diff --git a/exercise-003-web/exhibit-f/server_test.go b/exercise-003-web/exhibit-f/server_test.go new file mode 100644 index 0000000..38ea3ee --- /dev/null +++ b/exercise-003-web/exhibit-f/server_test.go @@ -0,0 +1,65 @@ +package main + +import ( + "github.com/stretchr/testify/assert" + "net/http" + "net/http/httptest" + "net/url" + "testing" +) + +func init() { + setup("../") +} + +func TestServer(t *testing.T) { + assert := assert.New(t) + + req, err := http.NewRequest("GET", "http://localhost:8080/home", nil) + assert.Nil(err) + + w := httptest.NewRecorder() + home(w, req) + + if status := w.Code; status != http.StatusOK { + t.Errorf("Status code differs. Expected %d .\n Got %d instead", http.StatusOK, status) + } + + assert.Contains(w.Body.String(), "Enter Name") + assert.Contains(w.Body.String(), "Username") + assert.Contains(w.Body.String(), "Attempts") +} + +func TestPost(t *testing.T) { + assert := assert.New(t) + + v := url.Values{} + + req, err := http.NewRequest("POST", "http://localhost:8080/home", nil) + v.Add("username", "Kavya") + req.PostForm = v + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + assert.Nil(err) + + w := httptest.NewRecorder() + home(w, req) + assert.Contains(w.Body.String(), "Kavya") + assert.Contains(w.Body.String(), "1") +} + +func TestConnect(t *testing.T) { + assert := assert.New(t) + + req, err := http.NewRequest("PUT", "http://localhost:8080/home", nil) + assert.Nil(err) + + w := httptest.NewRecorder() + home(w, req) + + if status := w.Code; status != http.StatusOK { + t.Errorf("Status code differs. Expected %d .\n Got %d instead", http.StatusOK, status) + } + + assert.Contains(w.Body.String(), "Invalid HTTP method") + +}