golang.org/x/build@v0.0.0-20240506185731-218518f32b70/cloudfns/wikiwebhook/wikiwebhook_test.go (about) 1 // Copyright 2019 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package wikiwebhook 6 7 import ( 8 "bytes" 9 "errors" 10 "io" 11 "net/http/httptest" 12 "testing" 13 ) 14 15 func TestValidSignature(t *testing.T) { 16 testCases := []struct { 17 body, key []byte 18 sig string 19 matches bool 20 }{ 21 {[]byte("body"), []byte("key"), "sha1=70bbf6819d1037aa94ca7e7f537cbea25fe49283", true}, 22 {[]byte("body"), []byte("key"), "sha1=70bbf6819d1037aa94ca7e7f537cbea25fe49284", false}, 23 {[]byte{}, []byte{}, "", false}, 24 {[]byte{}, []byte{}, "sha1=not a valid hex string", false}, 25 } 26 for _, tc := range testCases { 27 if matches := validSignature(tc.body, tc.key, tc.sig); matches != tc.matches { 28 t.Errorf("expected match = %v; got match = %v\nbody: %q, key: %q, sig: %q", tc.matches, matches, tc.body, tc.key, tc.sig) 29 } 30 } 31 } 32 33 func TestWebHook(t *testing.T) { 34 testCases := []struct { 35 desc string 36 body []byte 37 headers map[string]string 38 publishFn func(string, []byte) (string, error) 39 statusCode int 40 respBody []byte 41 }{ 42 { 43 "invalid signature", 44 nil, 45 map[string]string{ 46 "X-Hub-Signature": "sha1=invalid", 47 }, 48 nil, 49 401, 50 []byte("signature mismatch\n"), 51 }, 52 { 53 "ping event", 54 nil, 55 map[string]string{ 56 "X-Hub-Signature": "sha1=fbdb1d1b18aa6c08324b7d64b71fb76370690e1d", 57 "X-GitHub-Event": "ping", 58 }, 59 nil, 60 200, 61 []byte("pong"), 62 }, 63 { 64 "wiki change event", 65 []byte("body"), 66 map[string]string{ 67 "X-Hub-Signature": "sha1=cc5e6b2b046bc7401d071a3d9be9a1cf1869376d", 68 "X-GitHub-Event": "gollum", 69 }, 70 func(topic string, body []byte) (string, error) { 71 if got, want := body, []byte("body"); !bytes.Equal(got, want) { 72 t.Errorf("unexpected body: got %q; expected %q", got, want) 73 } 74 return "42", nil 75 }, 76 200, 77 []byte("Message ID: 42\n"), 78 }, 79 { 80 "error publishing topic", 81 nil, 82 map[string]string{ 83 "X-Hub-Signature": "sha1=fbdb1d1b18aa6c08324b7d64b71fb76370690e1d", 84 "X-GitHub-Event": "gollum", 85 }, 86 func(topic string, body []byte) (string, error) { 87 return "", errors.New("publishToTopic error") 88 }, 89 500, 90 []byte("publishToTopic error\n"), 91 }, 92 } 93 for _, tc := range testCases { 94 t.Run(tc.desc, func(t *testing.T) { 95 oldFn := publishToTopic 96 defer func() { publishToTopic = oldFn }() 97 publishToTopic = tc.publishFn 98 99 req := httptest.NewRequest("GET", "http://cloudfunctionz.com/func", bytes.NewReader(tc.body)) 100 for k, v := range tc.headers { 101 req.Header.Set(k, v) 102 } 103 w := httptest.NewRecorder() 104 GitHubWikiChangeWebHook(w, req) 105 106 resp := w.Result() 107 body, err := io.ReadAll(resp.Body) 108 if err != nil { 109 t.Errorf("io.ReadAll: %v", err) 110 } 111 if got, want := resp.StatusCode, tc.statusCode; got != want { 112 t.Errorf("Unexpected status code: got %d; want %d", got, want) 113 } 114 if !bytes.Equal(body, tc.respBody) { 115 t.Errorf("Unexpected body: got %q; want %q", body, tc.respBody) 116 } 117 }) 118 } 119 }