-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature - csv templating function - milestone 2
- Loading branch information
1 parent
2b2fdc9
commit 3a86e43
Showing
25 changed files
with
744 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package v2 | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/SpectoLabs/hoverfly/core/handlers" | ||
"github.com/codegangsta/negroni" | ||
"github.com/go-zoo/bone" | ||
"net/http" | ||
) | ||
|
||
type HoverflyTemplateDataSource interface { | ||
SetCsvDataSource(string, string) error | ||
DeleteDataSource(string) | ||
GetAllDataSources() TemplateDataSourceView | ||
} | ||
|
||
type HoverflyTemplateDataSourceHandler struct { | ||
Hoverfly HoverflyTemplateDataSource | ||
} | ||
|
||
func (templateDataSourceHandler HoverflyTemplateDataSourceHandler) RegisterRoutes(mux *bone.Mux, am *handlers.AuthHandler) { | ||
|
||
mux.Get("/api/v2/hoverfly/templating-data-source/csv", negroni.New( | ||
negroni.HandlerFunc(am.RequireTokenAuthentication), | ||
negroni.HandlerFunc(templateDataSourceHandler.Get), | ||
)) | ||
|
||
mux.Put("/api/v2/hoverfly/templating-data-source/csv", negroni.New( | ||
negroni.HandlerFunc(am.RequireTokenAuthentication), | ||
negroni.HandlerFunc(templateDataSourceHandler.Put), | ||
)) | ||
|
||
mux.Delete("/api/v2/hoverfly/templating-data-source/csv/:dataSourceName", negroni.New( | ||
negroni.HandlerFunc(am.RequireTokenAuthentication), | ||
negroni.HandlerFunc(templateDataSourceHandler.Delete), | ||
)) | ||
} | ||
|
||
func (templateDataSourceHandler HoverflyTemplateDataSourceHandler) Put(rw http.ResponseWriter, req *http.Request, next http.HandlerFunc) { | ||
var templateDataSourceRequest CSVDataSourceView | ||
err := handlers.ReadFromRequest(req, &templateDataSourceRequest) | ||
if err != nil { | ||
handlers.WriteErrorResponse(rw, err.Error(), 400) | ||
return | ||
} | ||
if err := templateDataSourceHandler.Hoverfly.SetCsvDataSource(templateDataSourceRequest.Name, templateDataSourceRequest.Data); err != nil { | ||
handlers.WriteErrorResponse(rw, err.Error(), 400) | ||
return | ||
} | ||
templateDataSourceHandler.Get(rw, req, next) | ||
} | ||
|
||
func (templateDataSourceHandler HoverflyTemplateDataSourceHandler) Delete(rw http.ResponseWriter, req *http.Request, next http.HandlerFunc) { | ||
dataSourceName := bone.GetValue(req, "dataSourceName") | ||
templateDataSourceHandler.Hoverfly.DeleteDataSource(dataSourceName) | ||
templateDataSourceHandler.Get(rw, req, next) | ||
} | ||
|
||
func (templateDataSourceHandler HoverflyTemplateDataSourceHandler) Get(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) { | ||
|
||
templateDataSourceView := templateDataSourceHandler.Hoverfly.GetAllDataSources() | ||
bytes, _ := json.Marshal(templateDataSourceView) | ||
|
||
handlers.WriteResponse(rw, bytes) | ||
} |
80 changes: 80 additions & 0 deletions
80
core/handlers/v2/hoverfly_templatedatasource_handler_test.go
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,80 @@ | ||
package v2 | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var csvDataSource = CSVDataSourceView{ | ||
"Test-CSV", | ||
"id,name,marks\n1,Test1,55\n2,Test2,56\n", | ||
} | ||
|
||
const path = "/api/v2/hoverfly/templating-data-source/csv" | ||
|
||
type HoverflyTemplateDataSourceStub struct{} | ||
|
||
func (HoverflyTemplateDataSourceStub) GetAllDataSources() TemplateDataSourceView { | ||
|
||
csvDataSources := []CSVDataSourceView{csvDataSource} | ||
return TemplateDataSourceView{DataSources: csvDataSources} | ||
} | ||
|
||
func (HoverflyTemplateDataSourceStub) SetCsvDataSource(string, string) error { | ||
return nil | ||
} | ||
|
||
func (HoverflyTemplateDataSourceStub) DeleteDataSource(string) { | ||
} | ||
|
||
func Test_TemplateDataSourceHandler_DeleteDataSource(t *testing.T) { | ||
RegisterTestingT(t) | ||
|
||
stubHoverfly := &HoverflyTemplateDataSourceStub{} | ||
unit := HoverflyTemplateDataSourceHandler{Hoverfly: stubHoverfly} | ||
|
||
request, err := http.NewRequest("DELETE", path+"/test-source", nil) | ||
Expect(err).To(BeNil()) | ||
|
||
response := makeRequestOnHandler(unit.Delete, request) | ||
Expect(response.Code).To(Equal(http.StatusOK)) | ||
} | ||
|
||
func Test_TemplateDataSourceHandler_SetDataSource(t *testing.T) { | ||
RegisterTestingT(t) | ||
|
||
stubHoverfly := &HoverflyTemplateDataSourceStub{} | ||
unit := HoverflyTemplateDataSourceHandler{Hoverfly: stubHoverfly} | ||
|
||
bodyBytes, err := json.Marshal(csvDataSource) | ||
Expect(err).To(BeNil()) | ||
|
||
request, err := http.NewRequest("PUT", path, ioutil.NopCloser(bytes.NewBuffer(bodyBytes))) | ||
Expect(err).To(BeNil()) | ||
|
||
response := makeRequestOnHandler(unit.Put, request) | ||
Expect(response.Code).To(Equal(http.StatusOK)) | ||
} | ||
|
||
func Test_TemplateDataSourceHandler_GetAllDataSources(t *testing.T) { | ||
RegisterTestingT(t) | ||
stubHoverfly := &HoverflyTemplateDataSourceStub{} | ||
unit := HoverflyTemplateDataSourceHandler{Hoverfly: stubHoverfly} | ||
|
||
request, err := http.NewRequest("GET", path, nil) | ||
Expect(err).To(BeNil()) | ||
|
||
response := makeRequestOnHandler(unit.Get, request) | ||
Expect(response.Code).To(Equal(http.StatusOK)) | ||
|
||
responseBody := response.Body.String() | ||
expectedResponseBodyBytes, _ := json.Marshal(TemplateDataSourceView{DataSources: []CSVDataSourceView{csvDataSource}}) | ||
|
||
Expect(responseBody).To(Equal(string(expectedResponseBodyBytes))) | ||
|
||
} |
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,10 @@ | ||
package v2 | ||
|
||
type TemplateDataSourceView struct { | ||
DataSources []CSVDataSourceView `json:"csvDataSources,omitempty"` | ||
} | ||
|
||
type CSVDataSourceView struct { | ||
Name string `json:"name"` | ||
Data string `json:"data"` | ||
} |
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
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
Oops, something went wrong.