func TestJsonPushEventError(t *testing.T) { server := httptest.NewServer(webhook.NewController(&okBuildConfigGetter{}, &okBuildConfigInstantiator{}, map[string]webhook.Plugin{"github": New()})) defer server.Close() post("X-GitHub-Event", "push", []byte{}, server.URL+"/build100/secret101/github", http.StatusBadRequest, t) }
func TestJsonGogsPushEvent(t *testing.T) { server := httptest.NewServer(webhook.NewController(&okBuildConfigGetter{}, &okBuildConfigInstantiator{}, map[string]webhook.Plugin{"github": New()})) defer server.Close() postFile("X-Gogs-Event", "push", "pushevent.json", server.URL+"/build100/secret101/github", http.StatusOK, t) }
func TestWrongMethod(t *testing.T) { server := httptest.NewServer(webhook.NewController(&okBuildConfigGetter{}, &okBuildConfigInstantiator{}, map[string]webhook.Plugin{"github": New()})) defer server.Close() resp, _ := http.Get(server.URL + "/build100/secret101/github") body, _ := ioutil.ReadAll(resp.Body) if resp.StatusCode != http.StatusBadRequest || !strings.Contains(string(body), "method") { t.Errorf("Expected BadRequest , got %s: %s!", resp.Status, string(body)) } }
func TestWrongSecret(t *testing.T) { server := httptest.NewServer(webhook.NewController(&okBuildConfigGetter{}, &okBuildConfigInstantiator{}, map[string]webhook.Plugin{"github": New()})) defer server.Close() client := &http.Client{} req, _ := http.NewRequest("POST", server.URL+"/build100/wrongsecret/github", nil) resp, _ := client.Do(req) body, _ := ioutil.ReadAll(resp.Body) if resp.StatusCode != http.StatusBadRequest || !strings.Contains(string(body), webhook.ErrSecretMismatch.Error()) { t.Errorf("Expected BadRequest, got %s: %s!", resp.Status, string(body)) } }
func TestMissingEvent(t *testing.T) { server := httptest.NewServer(webhook.NewController(&okBuildConfigGetter{}, &okBuildConfigInstantiator{}, map[string]webhook.Plugin{"github": New()})) defer server.Close() client := &http.Client{} req, _ := http.NewRequest("POST", server.URL+"/build100/secret101/github", nil) req.Header.Add("Content-Type", "application/json") resp, _ := client.Do(req) body, _ := ioutil.ReadAll(resp.Body) if resp.StatusCode != http.StatusBadRequest || !strings.Contains(string(body), "Missing X-GitHub-Event or X-Gogs-Event") { t.Errorf("Expected BadRequest, got %s: %s!", resp.Status, string(body)) } }