2017-01-09 17:22:10 -05:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2017 Minio, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2017-05-04 16:43:54 -04:00
|
|
|
"errors"
|
2017-01-09 17:22:10 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2017-08-12 22:25:43 -04:00
|
|
|
"os"
|
2017-01-09 17:22:10 -05:00
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Custom post handler to handle POST requests.
|
|
|
|
type postHandler struct{}
|
|
|
|
|
|
|
|
func (p postHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != "POST" {
|
|
|
|
http.Error(w, fmt.Sprintf("Unexpected method %s", r.Method), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
io.Copy(w, r.Body)
|
|
|
|
}
|
|
|
|
|
2017-05-04 16:43:54 -04:00
|
|
|
type errorHandler struct{}
|
|
|
|
|
|
|
|
func (e errorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.Error(w, fmt.Sprintf("Unexpected method %s", r.Method), http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
|
2017-01-09 17:22:10 -05:00
|
|
|
// Tests web hook initialization.
|
|
|
|
func TestNewWebHookNotify(t *testing.T) {
|
2017-01-18 15:24:34 -05:00
|
|
|
root, err := newTestConfig(globalMinioDefaultRegion)
|
2017-01-09 17:22:10 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-08-12 22:25:43 -04:00
|
|
|
defer os.RemoveAll(root)
|
2017-01-09 17:22:10 -05:00
|
|
|
|
|
|
|
_, err = newWebhookNotify("1")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Unexpected should fail")
|
|
|
|
}
|
|
|
|
|
2017-08-06 14:27:33 -04:00
|
|
|
serverConfig.Notify.SetWebhookByID("10", webhookNotify{Enable: true, Endpoint: "http://127.0.0.1:80"})
|
2017-01-09 17:22:10 -05:00
|
|
|
_, err = newWebhookNotify("10")
|
2017-05-04 16:43:54 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected should not fail with lookupEndpoint", err)
|
2017-01-09 17:22:10 -05:00
|
|
|
}
|
|
|
|
|
2017-02-09 18:20:54 -05:00
|
|
|
serverConfig.Notify.SetWebhookByID("15", webhookNotify{Enable: true, Endpoint: "http://%"})
|
2017-01-09 17:22:10 -05:00
|
|
|
_, err = newWebhookNotify("15")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Unexpected should fail with invalid URL escape")
|
|
|
|
}
|
|
|
|
|
|
|
|
server := httptest.NewServer(postHandler{})
|
|
|
|
defer server.Close()
|
|
|
|
|
2017-02-09 18:20:54 -05:00
|
|
|
serverConfig.Notify.SetWebhookByID("20", webhookNotify{Enable: true, Endpoint: server.URL})
|
2017-01-09 17:22:10 -05:00
|
|
|
webhook, err := newWebhookNotify("20")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected shouldn't fail", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
webhook.WithFields(logrus.Fields{
|
|
|
|
"Key": path.Join("bucket", "object"),
|
|
|
|
"EventType": "s3:ObjectCreated:Put",
|
|
|
|
}).Info()
|
|
|
|
}
|
2017-05-04 16:43:54 -04:00
|
|
|
|
|
|
|
// Add tests for lookup endpoint.
|
|
|
|
func TestLookupEndpoint(t *testing.T) {
|
|
|
|
server := httptest.NewServer(errorHandler{})
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
endpoint string
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
// Ignore endpoints which don't exist.
|
|
|
|
{
|
|
|
|
endpoint: "http://unknown",
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
endpoint: "%%%",
|
|
|
|
err: errors.New("parse %%%: invalid URL escape \"%%%\""),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
endpoint: server.URL,
|
2017-09-22 17:03:31 -04:00
|
|
|
err: fmt.Errorf("Unable to lookup webhook endpoint %s response(400 Bad Request)", server.URL),
|
2017-05-04 16:43:54 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range testCases {
|
|
|
|
if err := lookupEndpoint(test.endpoint); err != nil {
|
|
|
|
if err.Error() != test.err.Error() {
|
|
|
|
t.Errorf("Expected %s, got %s", test.err, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|