github.com/yunabe/lgo@v0.0.0-20190709125917-42c42d410fdf/jupyter/gojupyterscaffold/gojupyterscaffold_test.go (about) 1 package gojupyterscaffold 2 3 import ( 4 "encoding/json" 5 "testing" 6 ) 7 8 func TestVerifyHMac(t *testing.T) { 9 msgs := [][]byte{ 10 []byte("8307DB769F7E4B5F8B4D48735D36BDE3"), 11 []byte("<IDS|MSG>"), 12 []byte("ad1deb3813fcf7d3bdcf046d9a5c7276c87e4f0b98deb784413e0e36bc35e986"), 13 []byte("{\"username\":\"username\",\"version\":\"5.0\",\"msg_id\":\"DBAFA475AEE94EAE8860A5F46B561661\",\"msg_type\":\"kernel_info_request\",\"session\":\"8307DB769F7E4B5F8B4D48735D36BDE3\"}"), 14 []byte("{}"), 15 []byte("{}"), 16 []byte("{}"), 17 } 18 key := []byte("37485811-fb40116f79cb23af4056c7a8") 19 var msg message 20 err := msg.Unmarshal(msgs, key) 21 if err != nil { 22 t.Errorf("Unexpected error: %v", err) 23 } 24 if len(msg.Identity) != 1 { 25 t.Errorf("Unexpected size: %d", len(msg.Identity)) 26 } 27 if msg.Header.Username != "username" { 28 t.Errorf("Unexpected Username: %s", msg.Header.Username) 29 } 30 if msg.ParentHeader.Username != "" { 31 t.Errorf("Unexpected Username: %s", msg.Header.Username) 32 } 33 _, err = msg.Marshal(key) 34 if err != nil { 35 t.Errorf("Unexpected error: %v", err) 36 } 37 } 38 39 func TestMessageHeader(t *testing.T) { 40 var header messageHeader 41 err := json.Unmarshal([]byte("{\"username\":\"username\",\"version\":\"5.0\",\"msg_id\":\"DBAFA475AEE94EAE8860A5F46B561661\",\"msg_type\":\"kernel_info_request\",\"session\":\"8307DB769F7E4B5F8B4D48735D36BDE3\"}"), &header) 42 if err != nil { 43 t.Error(err) 44 } 45 expected := messageHeader{MsgID: "DBAFA475AEE94EAE8860A5F46B561661", Username: "username", Session: "8307DB769F7E4B5F8B4D48735D36BDE3", Date: "", MsgType: "kernel_info_request", Version: "5.0"} 46 if header != expected { 47 t.Errorf("Unexpected header: %#v", header) 48 } 49 }