Skip to content

Commit

Permalink
Add support for files (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
hudymi authored Sep 14, 2019
1 parent 0a67f5b commit cb15b93
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,7 @@ endpoints:
defaultResponseContent: "Sample service"
# Default response content-type, if not provided "text/plain; charset=utf-8"
defaultResponseContentType: text/plain; charset=utf-8
# Path to the file that is returned by default, if provided then defaultResponseContent is ignored
defaultResponseFile: "mockice/index.html"

```
40 changes: 36 additions & 4 deletions pkg/endpoint/endpoint.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package endpoint

import (
"io"
"io/ioutil"
"net/http"
"os"
"strings"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand All @@ -14,6 +18,7 @@ type Config struct {
DefaultResponseCode *int `yaml:"defaultResponseCode"`
DefaultResponseContent string `yaml:"defaultResponseContent"`
DefaultResponseContentType *string `yaml:"defaultResponseContentType"`
DefaultResponseFile *string `yaml:"defaultResponseFile"`
}

// DefaultConfig generates default configuration with /hello endpoint
Expand Down Expand Up @@ -51,17 +56,28 @@ func (e *Endpoint) Handle(writer http.ResponseWriter, request *http.Request) {
return
}

if e.config.DefaultResponseContentType != nil {
writer.Header().Set("Content-Type", *e.config.DefaultResponseContentType)
writer.Header().Set("Content-Type", e.defaultContentType())

var reader io.ReadCloser
if e.config.DefaultResponseFile != nil {
file, err := e.fileReader(writer)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
e.log.Error(err)
return
}

reader = file
} else {
writer.Header().Set("Content-Type", "text/plain; charset=utf-8")
reader = ioutil.NopCloser(strings.NewReader(e.config.DefaultResponseContent))
}
defer reader.Close()

if e.config.DefaultResponseCode != nil {
writer.WriteHeader(*e.config.DefaultResponseCode)
}

_, err := writer.Write([]byte(e.config.DefaultResponseContent))
_, err := io.Copy(writer, reader)
if err != nil {
err = errors.Wrapf(err, "while writing response")
http.Error(writer, err.Error(), http.StatusInternalServerError)
Expand All @@ -70,6 +86,22 @@ func (e *Endpoint) Handle(writer http.ResponseWriter, request *http.Request) {
}
}

func (e *Endpoint) defaultContentType() string {
if e.config.DefaultResponseContentType != nil {
return *e.config.DefaultResponseContentType
}
return "text/plain; charset=utf-8"
}

func (e *Endpoint) fileReader(writer io.Writer) (io.ReadCloser, error) {
file, err := os.Open(*e.config.DefaultResponseFile)
if err != nil {
return nil, errors.Wrapf(err, "while opening file %s", *e.config.DefaultResponseFile)
}

return file, nil
}

// Name returns name of the endpoint
func (e *Endpoint) Name() string {
return e.config.Name
Expand Down
49 changes: 48 additions & 1 deletion pkg/endpoint/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package endpoint_test
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -105,6 +106,37 @@ func TestEndpoint_Handle(t *testing.T) {
DefaultResponseCode: intPointer(http.StatusNotModified),
},
},
"With file": {
method: http.MethodGet,
expectedResponseCode: http.StatusOK,
config: endpoint.Config{
Name: "simple",
DefaultResponseCode: intPointer(http.StatusOK),
DefaultResponseContentType: stringPointer("text/markdown; charset=utf-8"),
DefaultResponseFile: stringPointer("testdata/test.md"),
},
},
"With file and content": {
method: http.MethodGet,
expectedResponseCode: http.StatusOK,
config: endpoint.Config{
Name: "simple",
DefaultResponseContent: "test",
DefaultResponseCode: intPointer(http.StatusOK),
DefaultResponseContentType: stringPointer("text/markdown; charset=utf-8"),
DefaultResponseFile: stringPointer("testdata/test.md"),
},
},
"With not existing file": {
method: http.MethodGet,
expectedResponseCode: http.StatusInternalServerError,
config: endpoint.Config{
Name: "simple",
DefaultResponseCode: intPointer(http.StatusOK),
DefaultResponseContentType: stringPointer("text/markdown; charset=utf-8"),
DefaultResponseFile: stringPointer("testdata/test.md.fail"),
},
},
} {
t.Run(testName, func(t *testing.T) {
// Given
Expand Down Expand Up @@ -133,11 +165,26 @@ func TestEndpoint_Handle(t *testing.T) {

buff := new(bytes.Buffer)
buff.ReadFrom(recorder.Result().Body)
g.Expect(buff.String()).To(Equal(testCase.config.DefaultResponseContent))
if testCase.config.DefaultResponseFile == nil {
g.Expect(buff.String()).To(Equal(testCase.config.DefaultResponseContent))
} else {
content, err := readFile(*testCase.config.DefaultResponseFile)
g.Expect(err).To(Succeed())
g.Expect(buff.String()).To(Equal(content))
}
})
}
}

func readFile(path string) (string, error) {
content, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}

return string(content), nil
}

func stringPointer(value string) *string {
return &value
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/endpoint/testdata/test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Header

Text

0 comments on commit cb15b93

Please sign in to comment.