-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complete extraction of routes and refactoring
- Loading branch information
1 parent
52aa320
commit 9ca375f
Showing
7 changed files
with
115 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
func (s *server) handleBinInspect() http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
switch r.Method { | ||
case "GET": | ||
binID := r.URL.Path[len("/inspect/"):] | ||
|
||
bin, exists := s.db.FindBin(binID) | ||
|
||
if !exists { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
|
||
s.renderTemplate(w, "inspect", &bin) | ||
return | ||
default: | ||
http.NotFound(w, r) | ||
return | ||
} | ||
} | ||
} |
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,57 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/matryer/is" | ||
) | ||
|
||
func TestBinInspect(t *testing.T) { | ||
is := is.New(t) | ||
|
||
srv, err := newServer() | ||
srv.db.Connect() | ||
defer srv.db.Disconnect() | ||
is.NoErr(err) // newServer error | ||
|
||
hsrv := httptest.NewServer(srv) | ||
defer hsrv.Close() | ||
|
||
req, err := http.NewRequest("POST", hsrv.URL+"/new/", nil) | ||
is.NoErr(err) // NewRequest error | ||
client := &http.Client{ | ||
Timeout: 1 * time.Minute, | ||
} | ||
|
||
resp, err := client.Do(req) | ||
is.NoErr(err) // Client request error | ||
is.Equal(resp.StatusCode, 200) // Making new bin gives 200 | ||
|
||
inspectURL := hsrv.URL + "/inspect/" | ||
binID := resp.Request.URL.String()[len(inspectURL):] | ||
|
||
var reqBody = []byte(`{"request":"Golang test request"}`) | ||
req, err = http.NewRequest("POST", hsrv.URL+"/r/"+binID, bytes.NewBuffer(reqBody)) | ||
is.NoErr(err) // New Request error | ||
req.Header.Set("X-Custom-Header", "custom value 2468") | ||
req.Header.Set("Content-type", "application/json") | ||
resp, err = client.Do(req) | ||
is.NoErr(err) // Client request error | ||
is.Equal(resp.StatusCode, 200) // Initial request returns 200 | ||
|
||
req, err = http.NewRequest("GET", inspectURL+binID, nil) | ||
is.NoErr(err) // New Request error | ||
resp, err = client.Do(req) | ||
is.NoErr(err) // Client request error | ||
is.Equal(resp.StatusCode, 200) // Inspect returns 200 | ||
body, err := ioutil.ReadAll(resp.Body) | ||
is.NoErr(err) // body read error | ||
is.True(strings.Contains(string(body), "custom value 2468")) | ||
is.True(strings.Contains(string(body), "Golang test request")) | ||
} |
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,19 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
func (s *server) handleRequest() http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
binID := r.URL.Path[len("/r/"):] | ||
fmt.Println("Request made: ", binID) | ||
|
||
if err := s.db.AddRekwest(binID, r); err == nil { | ||
fmt.Fprintf(w, "Request saved. (ip: %s)", r.RemoteAddr) | ||
} else { | ||
http.NotFound(w, r) | ||
} | ||
} | ||
} |
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