| 
									
										
										
										
											2017-01-10 06:22:10 +08: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-05 04:43:54 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2017-01-10 06:22:10 +08:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"io" | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 	"net/http/httptest" | 
					
						
							| 
									
										
										
										
											2017-08-13 10:25:43 +08:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2017-01-10 06:22:10 +08: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-05 04:43:54 +08: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-10 06:22:10 +08:00
										 |  |  | // Tests web hook initialization.
 | 
					
						
							|  |  |  | func TestNewWebHookNotify(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2017-01-19 04:24:34 +08:00
										 |  |  | 	root, err := newTestConfig(globalMinioDefaultRegion) | 
					
						
							| 
									
										
										
										
											2017-01-10 06:22:10 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-08-13 10:25:43 +08:00
										 |  |  | 	defer os.RemoveAll(root) | 
					
						
							| 
									
										
										
										
											2017-01-10 06:22:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	_, err = newWebhookNotify("1") | 
					
						
							|  |  |  | 	if err == nil { | 
					
						
							|  |  |  | 		t.Fatal("Unexpected should fail") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-07 02:27:33 +08:00
										 |  |  | 	serverConfig.Notify.SetWebhookByID("10", webhookNotify{Enable: true, Endpoint: "http://127.0.0.1:80"}) | 
					
						
							| 
									
										
										
										
											2017-01-10 06:22:10 +08:00
										 |  |  | 	_, err = newWebhookNotify("10") | 
					
						
							| 
									
										
										
										
											2017-05-05 04:43:54 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Unexpected should not fail with lookupEndpoint", err) | 
					
						
							| 
									
										
										
										
											2017-01-10 06:22:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-10 07:20:54 +08:00
										 |  |  | 	serverConfig.Notify.SetWebhookByID("15", webhookNotify{Enable: true, Endpoint: "http://%"}) | 
					
						
							| 
									
										
										
										
											2017-01-10 06:22:10 +08: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-10 07:20:54 +08:00
										 |  |  | 	serverConfig.Notify.SetWebhookByID("20", webhookNotify{Enable: true, Endpoint: server.URL}) | 
					
						
							| 
									
										
										
										
											2017-01-10 06:22:10 +08: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-05 04:43:54 +08: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-23 05:03:31 +08:00
										 |  |  | 			err:      fmt.Errorf("Unable to lookup webhook endpoint %s response(400 Bad Request)", server.URL), | 
					
						
							| 
									
										
										
										
											2017-05-05 04:43:54 +08: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) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |