http-util_test.go (1978B)
1 // This file is part of taler-cashless2ecash. 2 // Copyright (C) 2024 Joel Häberli 3 // 4 // taler-cashless2ecash is free software: you can redistribute it and/or modify it 5 // under the terms of the GNU Affero General Public License as published 6 // by the Free Software Foundation, either version 3 of the License, 7 // or (at your option) any later version. 8 // 9 // taler-cashless2ecash is distributed in the hope that it will be useful, but 10 // WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 // Affero General Public License for more details. 13 // 14 // You should have received a copy of the GNU Affero General Public License 15 // along with this program. If not, see <http://www.gnu.org/licenses/>. 16 // 17 // SPDX-License-Identifier: AGPL3.0-or-later 18 19 package internal_utils 20 21 import ( 22 "fmt" 23 "testing" 24 ) 25 26 const URL_GET = "https://jsonplaceholder.typicode.com/todos/:id" 27 const URL_POST = "https://jsonplaceholder.typicode.com/posts" 28 29 type TestStruct struct { 30 UserId int `json:"userId"` 31 Id int `json:"id"` 32 Title string `json:"title"` 33 Completed bool `json:"completed"` 34 } 35 36 func TestGET(t *testing.T) { 37 38 url := FormatUrl( 39 URL_GET, 40 map[string]string{ 41 "id": "1", 42 }, 43 map[string]string{}, 44 ) 45 46 codec := NewJsonCodec[TestStruct]() 47 res, status, err := HttpGet( 48 url, 49 map[string]string{}, 50 codec, 51 ) 52 53 if err != nil { 54 t.Errorf("%s", err.Error()) 55 t.FailNow() 56 } 57 58 fmt.Println("res:", res, ", status:", status) 59 } 60 61 func TestPOST(t *testing.T) { 62 63 url := FormatUrl( 64 URL_POST, 65 map[string]string{ 66 "id": "1", 67 }, 68 map[string]string{}, 69 ) 70 71 res, status, err := HttpPost( 72 url, 73 map[string]string{}, 74 &TestStruct{ 75 UserId: 1, 76 Id: 1, 77 Title: "TEST", 78 Completed: false, 79 }, 80 NewJsonCodec[TestStruct](), 81 NewJsonCodec[TestStruct](), 82 ) 83 84 if err != nil { 85 t.Errorf("%s", err.Error()) 86 t.FailNow() 87 } 88 89 fmt.Println("res:", res, ", status:", status) 90 }