github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/utils/httpserverutils_test.go (about) 1 /* 2 Copyright 2023. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package utils 18 19 import ( 20 "bytes" 21 "compress/gzip" 22 "encoding/base64" 23 "fmt" 24 "testing" 25 26 "github.com/cespare/xxhash" 27 "github.com/stretchr/testify/assert" 28 "github.com/valyala/fasthttp" 29 ) 30 31 func Test_VerifyBasicAuth(t *testing.T) { 32 const password = "hello" 33 const username = "world" 34 passwordHash := xxhash.Sum64String(password) 35 usernameHash := xxhash.Sum64String(username) 36 ctx := &fasthttp.RequestCtx{} 37 38 // Test invalid case where no authorization is provided. 39 assert.False(t, VerifyBasicAuth(ctx, usernameHash, passwordHash)) 40 41 // Test invalid case where the "Basic" prefix is missing. 42 encoded := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%v:%v", username, password))) 43 ctx.Request.Header.Set("Authorization", encoded) 44 assert.False(t, VerifyBasicAuth(ctx, usernameHash, passwordHash)) 45 46 // Test invalid case where the username is wrong. 47 encoded = base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("badUsername:%v", password))) 48 ctx.Request.Header.Set("Authorization", "Basic "+encoded) 49 assert.False(t, VerifyBasicAuth(ctx, usernameHash, passwordHash)) 50 51 // Test invalid case where the password is wrong. 52 encoded = base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%v:badPassword", username))) 53 ctx.Request.Header.Set("Authorization", "Basic "+encoded) 54 assert.False(t, VerifyBasicAuth(ctx, usernameHash, passwordHash)) 55 56 // Test invalid case where username and password are both wrong. 57 encoded = base64.StdEncoding.EncodeToString([]byte("badUsername:badPassword")) 58 ctx.Request.Header.Set("Authorization", "Basic "+encoded) 59 assert.False(t, VerifyBasicAuth(ctx, usernameHash, passwordHash)) 60 61 // Test invalid case where the colon is missing. 62 encoded = base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%v%v", username, password))) 63 ctx.Request.Header.Set("Authorization", "Basic "+encoded) 64 assert.False(t, VerifyBasicAuth(ctx, usernameHash, passwordHash)) 65 66 // Test a valid case. 67 encoded = base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%v:%v", username, password))) 68 ctx.Request.Header.Set("Authorization", "Basic "+encoded) 69 assert.True(t, VerifyBasicAuth(ctx, usernameHash, passwordHash)) 70 } 71 72 func Test_GetDecodedBody(t *testing.T) { 73 const body = "hello world" 74 ctx := &fasthttp.RequestCtx{} 75 76 // Test when the body is not encoded. 77 ctx.Request.SetBodyString(body) 78 79 decodedBody, err := GetDecodedBody(ctx) 80 assert.Nil(t, err) 81 assert.Equal(t, body, string(decodedBody)) 82 83 // Test when the body is gzipped. 84 buf := bytes.Buffer{} 85 writer := gzip.NewWriter(&buf) 86 _, err = writer.Write([]byte(body)) 87 assert.Nil(t, err) 88 err = writer.Close() 89 assert.Nil(t, err) 90 gzippedBody := buf.Bytes() 91 ctx.Request.Header.Set("Content-Encoding", "gzip") 92 ctx.Request.SetBody(gzippedBody) 93 94 decodedBody, err = GetDecodedBody(ctx) 95 assert.Nil(t, err) 96 assert.Equal(t, body, string(decodedBody)) 97 98 // Test an invalid encoding. 99 ctx.Request.Header.Set("Content-Encoding", "invalid") 100 decodedBody, err = GetDecodedBody(ctx) 101 assert.NotNil(t, err) 102 assert.Nil(t, decodedBody) 103 } 104 105 func Test_ExtractSeriesOfJsonObjects(t *testing.T) { 106 const body = `{"a": 1}{ 107 "b": 2, 108 "c": "crabs"} 109 {"d": 3}` 110 111 jsonObjects, err := ExtractSeriesOfJsonObjects([]byte(body)) 112 assert.Nil(t, err) 113 assert.Equal(t, 3, len(jsonObjects)) 114 assert.Equal(t, map[string]interface{}{"a": float64(1)}, jsonObjects[0]) 115 assert.Equal(t, map[string]interface{}{"b": float64(2), "c": "crabs"}, jsonObjects[1]) 116 assert.Equal(t, map[string]interface{}{"d": float64(3)}, jsonObjects[2]) 117 118 // Test invalid JSON. 119 const invalidBody = `{"a": 1}{` 120 jsonObjects, err = ExtractSeriesOfJsonObjects([]byte(invalidBody)) 121 assert.NotNil(t, err) 122 assert.Nil(t, jsonObjects) 123 }