Skip to content

Commit

Permalink
Create endpoint Schema from string
Browse files Browse the repository at this point in the history
  • Loading branch information
caalberts committed Apr 11, 2018
1 parent 6bdd8f4 commit 4c10312
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
7 changes: 7 additions & 0 deletions localghost.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package localghost

type Schema struct {
Method string
Path string
StatusCode int
}
23 changes: 23 additions & 0 deletions schema/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package schema

import (
"regexp"
"strconv"

"github.com/caalberts/localghost"
)

func FromString(definition string) (localghost.Schema, error) {
regex := "^(GET) (/) (\\d{3})$"
matches := regexp.MustCompile(regex).FindStringSubmatch(definition)

method := matches[1]
path := matches[2]
code, _ := strconv.Atoi(matches[3])
schema := localghost.Schema{
Method: method,
Path: path,
StatusCode: code,
}
return schema, nil
}
21 changes: 21 additions & 0 deletions schema/string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package schema

import (
"net/http"
"testing"

"github.com/caalberts/localghost"
"github.com/stretchr/testify/assert"
)

func TestFromString(t *testing.T) {
var definition string
var schema localghost.Schema

definition = "GET / 200"
schema, err := FromString(definition)
assert.Nil(t, err)
assert.Equal(t, "GET", schema.Method)
assert.Equal(t, "/", schema.Path)
assert.Equal(t, http.StatusOK, schema.StatusCode)
}

0 comments on commit 4c10312

Please sign in to comment.