-
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.
- Loading branch information
Showing
47 changed files
with
19,096 additions
and
8 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
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,79 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"math/rand" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type response struct { | ||
Status string `json:"status"` | ||
ShortName string `json:"short"` | ||
URL string `json:"url"` | ||
} | ||
|
||
type testingSuite struct { | ||
TestBed *testing.T | ||
Router *gin.Engine | ||
} | ||
|
||
func TestShortURLService(t *testing.T) { | ||
tb := testingSuite{ | ||
TestBed: t, | ||
Router: setupRouter(), | ||
} | ||
for i := 0; i < 10; i++ { | ||
originalURL := "https://" + randomString(30) | ||
shortURL := tb.requestShortURL(originalURL) | ||
URL := tb.requestLongURL(shortURL) | ||
assert.Equal(t, originalURL, URL) | ||
} | ||
|
||
} | ||
func (tb testingSuite) requestShortURL(originalURL string) string { | ||
gin.SetMode(gin.TestMode) | ||
postBody, _ := json.Marshal(map[string]string{ | ||
"url": originalURL, | ||
}) | ||
requestBody := bytes.NewBuffer(postBody) | ||
req, _ := http.NewRequest("POST", "/add", requestBody) | ||
req.Header.Add("Content-Type", "application/json") | ||
|
||
resp := httptest.NewRecorder() | ||
tb.Router.ServeHTTP(resp, req) | ||
assert.Equal(tb.TestBed, 200, resp.Code) | ||
|
||
body, _ := ioutil.ReadAll(resp.Body) | ||
var respBody response | ||
json.Unmarshal(body, &respBody) | ||
|
||
return respBody.ShortName | ||
} | ||
|
||
func (tb testingSuite) requestLongURL(shortURL string) string { | ||
// get | ||
req, _ := http.NewRequest("GET", "/"+shortURL, nil) | ||
|
||
resp := httptest.NewRecorder() | ||
tb.Router.ServeHTTP(resp, req) | ||
assert.Equal(tb.TestBed, 200, resp.Code) | ||
body, _ := ioutil.ReadAll(resp.Body) | ||
var respBody response | ||
json.Unmarshal(body, &respBody) | ||
return respBody.URL | ||
} | ||
func randomString(length int) string { | ||
rand.Seed(time.Now().UnixNano()) | ||
b := make([]byte, length) | ||
rand.Read(b) | ||
return fmt.Sprintf("%x", b)[:length] | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.