-
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.
Merge pull request #22 from ferry-go/feature/core/added-utilities
Feature/core/added utilities
- Loading branch information
Showing
5 changed files
with
203 additions
and
103 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,129 @@ | ||
package ferry | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
const ( | ||
GET = http.MethodGet | ||
POST = http.MethodPost | ||
PUT = http.MethodPut | ||
DELETE = http.MethodDelete | ||
|
||
// headers | ||
ContentType = "Content-Type" | ||
ContentDeposition = "Content-Disposition" | ||
ApplicationJson = "application/json" | ||
Attachment = "attachment" | ||
|
||
routerRegexReplace = "[a-zA-Z0-9_-]*" | ||
|
||
// routing error messages | ||
NotFoundMessage = "Not Found, Check URL" | ||
) | ||
|
||
// Finds wild card in URL and replace them with a regex for, | ||
// ex if path is /auth/:name -> /auth/[a-zA-Z0-9]* | ||
// ex if path is /auth/name -> /auth/name | ||
func findAndReplace(path string) string { | ||
if !strings.Contains(path, ":") { | ||
return fmt.Sprintf("%s%s%s", "^", path, "$") | ||
} | ||
result := "" | ||
slitted := strings.Split(path, "/") | ||
for _, v := range slitted { | ||
if v == "" { | ||
continue | ||
} | ||
if strings.Contains(v, ":") { | ||
result = fmt.Sprintf("%s/%s", result, routerRegexReplace) | ||
continue | ||
} | ||
result = fmt.Sprintf("%s/%s", result, v) | ||
} | ||
// replace slashes | ||
result = strings.ReplaceAll(result, "/", "\\/") | ||
result = fmt.Sprintf("%s%s%s", "^", result, "$") | ||
return result | ||
} | ||
|
||
// routerPath /auth/:name | ||
// requestPath /auth/madhuri | ||
// paramName name | ||
// returns madhuri | ||
func extractParamFromPath(routerPath, requestPath, paramName string) string { | ||
routerSplit := strings.Split(routerPath, "/") | ||
requestSplit := strings.Split(requestPath, "/") | ||
if len(routerSplit) != len(requestSplit) { | ||
return "" | ||
} | ||
paramWithWildCard := fmt.Sprintf(":%s", paramName) | ||
for k, v := range routerSplit { | ||
if v == paramWithWildCard { | ||
return requestSplit[k] | ||
} | ||
} | ||
return "" | ||
} | ||
|
||
// routerPath /auth/:name/:age | ||
// requestPath /auth/madhuri/32 | ||
// returns { name: madhuri, age: 32 } | ||
func getParamsFromPath(routerPath, requestPath string) map[string]string { | ||
paramsMap := map[string]string{} | ||
routerSplit := strings.Split(routerPath, "/") | ||
requestSplit := strings.Split(requestPath, "/") | ||
for k, v := range routerSplit { | ||
if strings.Contains(v, ":") { | ||
key := strings.ReplaceAll(v, ":", "") | ||
paramsMap[key] = requestSplit[k] | ||
} | ||
} | ||
return paramsMap | ||
} | ||
|
||
// returns value of a single query Param | ||
// | ||
// route path /hello?key=test&value=bbp | ||
// | ||
// keyValue = GetQueryParam(key) | ||
// | ||
// keyValue = test | ||
func getAllQueryParams(queryPath string) map[string]string { | ||
queryParamsMap := map[string]string{} | ||
params := strings.Split(queryPath, "&") | ||
for _, v := range params { | ||
if strings.Contains(v, "=") { | ||
pair := strings.Split(v, "=") | ||
queryParamsMap[pair[0]] = pair[1] | ||
} | ||
} | ||
return queryParamsMap | ||
} | ||
|
||
// returns map of query Params | ||
// | ||
// route path /hello?key=test&value=bbp | ||
// | ||
// returns {key : test, value : bbp} | ||
func getQueryParam(querypath string, name string) string { | ||
params := strings.Split(querypath, "&") | ||
for _, v := range params { | ||
if strings.Contains(v, "=") { | ||
pair := strings.Split(v, "=") | ||
if pair[0] == name { | ||
return pair[1] | ||
} | ||
} | ||
} | ||
return "" | ||
} | ||
|
||
func getAttachmentHeader(fileName string) string { | ||
if fileName == "" { | ||
return fmt.Sprintf(Attachment) | ||
} | ||
return fmt.Sprintf("%s; filename=%s", Attachment, fileName) | ||
} |
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,65 @@ | ||
package ferry | ||
|
||
import ( | ||
"fmt" | ||
"github.com/go-playground/assert/v2" | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
func TestAttachmentHeader(t *testing.T) { | ||
fileName := "hey" | ||
attachment := getAttachmentHeader(fileName) | ||
attachmentExpected := fmt.Sprintf("%s; filename=%s", Attachment, fileName) | ||
assert.Equal(t, attachment, attachmentExpected) | ||
} | ||
|
||
func TestPathParamRegex(t *testing.T) { | ||
pathParam := "/auth/:name" | ||
routerParam := "/auth/ferry" | ||
regexParam := findAndReplace(pathParam) | ||
assert.MatchRegex(t, routerParam, regexParam) | ||
} | ||
|
||
func TestPathParamRegexAbs(t *testing.T) { | ||
pathParam := "/auth/ferry" | ||
routerParam := "/auth/ferry" | ||
regexParam := findAndReplace(pathParam) | ||
assert.MatchRegex(t, routerParam, regexParam) | ||
} | ||
|
||
func TestPathParamRegexFail(t *testing.T) { | ||
pathParam := "/auth/:name" | ||
routerParam := "/auth/ferry/ss" | ||
regexParam := findAndReplace(pathParam) | ||
fail, _ := regexp.MatchString(routerParam, regexParam) | ||
assert.Equal(t, false, fail) | ||
} | ||
|
||
func TestGetParam(t *testing.T) { | ||
name := "ferry" | ||
pathParam := "/auth/:name/hello/:age" | ||
routerParam := fmt.Sprintf("/auth/%s/hello/%d", name, 1) | ||
wantedName := extractParamFromPath(pathParam, routerParam, "name") | ||
assert.Equal(t, wantedName, name) | ||
} | ||
|
||
func TestGetParamEmpty(t *testing.T) { | ||
name := "" | ||
pathParam := "/auth/:name" | ||
routerParam := "/auth/" + name | ||
wantedName := extractParamFromPath(pathParam, routerParam, "names") | ||
assert.Equal(t, wantedName, name) | ||
} | ||
|
||
func TestGetParams(t *testing.T) { | ||
name := "ferry" | ||
pathParam := "/auth/:name/hello/:age" | ||
routerParam := fmt.Sprintf("/auth/%s/hello/%d", name, 1) | ||
paramsMap := getParamsFromPath(pathParam, routerParam) | ||
wantedParamsMap := map[string]string{ | ||
"name": name, | ||
"age": "1", | ||
} | ||
assert.Equal(t, paramsMap, wantedParamsMap) | ||
} |