webp_server_go/helper/metadata_test.go
Benny a7b5992662
Metadata (#251)
* recover middleware

* simplify Atoi

* metadata data prototype

* InterestingAttention

* resize itself

* Bump version to 0.9.4
Added some comments
Removed String() for Extraparams

* Add metadata test

* Fix CI

* Remove unnecessary tests

* Update file count

* use t.Run to get test case

---------

Co-authored-by: n0vad3v <n0vad3v@riseup.net>
2023-07-11 19:08:32 +02:00

44 lines
1.4 KiB
Go

package helper
import (
"net/url"
"path"
"testing"
"webp_server_go/config"
)
func TestGetId(t *testing.T) {
p := "https://example.com/image.jpg?width=200&height=300"
t.Run("proxy mode", func(t *testing.T) {
// Test case 1: Proxy mode
config.ProxyMode = true
id, jointPath, santizedPath := getId(p)
// Verify the return values
expectedId := HashString(p)
expectedPath := ""
expectedSantizedPath := ""
if id != expectedId || jointPath != expectedPath || santizedPath != expectedSantizedPath {
t.Errorf("Test case 1 failed: Expected (%s, %s, %s), but got (%s, %s, %s)",
expectedId, expectedPath, expectedSantizedPath, id, jointPath, santizedPath)
}
})
t.Run("non-proxy mode", func(t *testing.T) {
// Test case 2: Non-proxy mode
config.ProxyMode = false
p = "/image.jpg?width=400&height=500"
id, jointPath, santizedPath := getId(p)
// Verify the return values
parsed, _ := url.Parse(p)
expectedId := HashString(parsed.Path + "?width=400&height=500")
expectedPath := path.Join(config.Config.ImgPath, parsed.Path)
expectedSantizedPath := parsed.Path + "?width=400&height=500"
if id != expectedId || jointPath != expectedPath || santizedPath != expectedSantizedPath {
t.Errorf("Test case 2 failed: Expected (%s, %s, %s), but got (%s, %s, %s)",
expectedId, expectedPath, expectedSantizedPath, id, jointPath, santizedPath)
}
})
}