Skip to content

Commit

Permalink
Merge branch 'master' of github.com:framps/raspiBackup
Browse files Browse the repository at this point in the history
  • Loading branch information
framps committed Jul 30, 2018
2 parents a98c380 + dc3ac64 commit 5882829
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
9 changes: 6 additions & 3 deletions RESTAPI/raspiBackupRESTListener.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,15 @@ func QueryHandler(c *gin.Context) {
func ParamHandler(c *gin.Context) {

param := c.Param("param")
optional := c.Param("optional")

fmt.Printf("Param: %s Optional: %s\n", param, optional)

if len(param) == 0 {
c.JSON(400, gin.H{"param": param, "exists": false})
c.JSON(400, gin.H{"param": param, "exists": false, "optional": optional})
return
}
c.JSON(200, gin.H{"param": param, "exists": true})
c.JSON(200, gin.H{"param": param, "exists": true, "optional": optional})
}

// BackupHandler - handles requests for raspiBackup
Expand Down Expand Up @@ -222,7 +225,7 @@ func NewEngine(passwordSet bool, credentialMap gin.Accounts) *gin.Engine {
v1.POST("/raspiBackup", BackupHandler)
v1.GET("/raspiBackup", VersionHandler)
v1.GET("/raspiBackup/query", QueryHandler)
v1.GET("/raspiBackup/param/:param", ParamHandler)
v1.GET("/raspiBackup/param/:param/*optional", ParamHandler)

return api
}
Expand Down
18 changes: 11 additions & 7 deletions RESTAPI/raspiBackup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ func TestParam(t *testing.T) {
}

// mock only works if a http client is used
oldHost := os.Getenv("HOST")
os.Setenv("HOST", "http://localhost:8080")
defer os.Setenv("HOST", oldHost)
//oldHost := os.Getenv("HOST")
//os.Setenv("HOST", "http://localhost:8080")
//defer os.Setenv("HOST", oldHost)

httpmock.Activate()
httpmock.RegisterNoResponder(httpmock.InitialTransport.RoundTrip) // non mocked urls are passed through
Expand All @@ -274,20 +274,24 @@ func TestParam(t *testing.T) {

// CALL endpoint
var b []byte
w, body, err := performer.PerformRequest(t, "GET", "/v1/raspiBackup/param/:param", bytes.NewBuffer(b))
w, body, err := performer.PerformRequest(t, "GET", "/v1/raspiBackup/param/42%2F4711%2f1147", bytes.NewBuffer(b))
require.NoError(t, err, "GET failed")

// DECODE response
type parmResponse struct {
value string
exists bool
Param string
Exists bool
Optional string
}

var r parmResponse
t.Logf("Payload: %s", string(*body))
err = json.Unmarshal(*body, &r)
require.NoError(t, err, "unmarshal failed")
t.Logf("Payload received: %s", string(*body))

// TEST response
assert.Equal(t, r.Param, "42")
assert.Equal(t, r.Optional, "/4711/1147")
assert.Equal(t, http.StatusOK, w.StatusCode)
t.Logf("JSON Response: %+v", r)
}

0 comments on commit 5882829

Please sign in to comment.