Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exercise 003 web v1 #129

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions exercise-003-web/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions exercise-003-web/.idea/exercise-003-web.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions exercise-003-web/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions exercise-003-web/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions exercise-003-web/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

366 changes: 366 additions & 0 deletions exercise-003-web/.idea/workspace.xml

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions exercise-003-web/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
gofmt -w .
golint ./...
go run exhibit-f/server.go
1 change: 1 addition & 0 deletions exercise-003-web/exhibit-c/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
)

// View structure
type View struct {
Name string
Age int
Expand Down
16 changes: 8 additions & 8 deletions exercise-003-web/exhibit-d/home.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<!DOCTYPE html>
<html>
<title>Learning Go</title>
<title>Learning Go</title>

<body>
<form action="/signup" method="POST">
<input type="text" name="username" placeholder="Enter Name">
<input type="submit" value="Sign-Up"/>
</form>
</body>
</html>
<body>
<form action="/signup" method="POST">
<input type="text" name="username" placeholder="Enter Name">
<input type="submit" value="Sign-Up"/>
</form>
</body>
</html>
2 changes: 1 addition & 1 deletion exercise-003-web/exhibit-e/server_test.go
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
23 changes: 23 additions & 0 deletions exercise-003-web/exhibit-f/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<title>Learning Go</title>

<body>
<form action="/home" method="POST">
<input type="text" name="username" placeholder="Enter Name">
<input type="submit" value="Sign-Up"/>
</form>
<table>
<tr>
<th>Username</th>
<th>Attempts</th>
</tr>
{{range $key, $value := .}}
<tr>
<td><strong>{{$key}}</strong></td>
<td><strong>{{$value}}</strong></td>
</tr>
{{end}}
</table>
</body>
</html>
47 changes: 47 additions & 0 deletions exercise-003-web/exhibit-f/server.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
65 changes: 65 additions & 0 deletions exercise-003-web/exhibit-f/server_test.go
Original file line number Diff line number Diff line change
@@ -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")

}