Tôi đang làm việc trên API Go chuyển giữa một chương trình phụ trợ nội bộ và một số API của bên thứ ba. Tôi đang cố gắng để hiểu làm thế nào để kiểm tra chức năng của nó mà không thực sự nhấn một API bên ngoài.Yêu cầu gửi đi thử nghiệm Go (Golang) mà không thực sự truy cập API của bên thứ ba
Ví dụ, đây là một máy chủ để xử lý yêu cầu đến để làm cho bài hát mới, và gửi yêu cầu đến một API của bên thứ ba:
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
var ThirdPartyApi = "http://www.coolsongssite.api"
type IncomingRequest struct {
username string `json:"username"`
password string `json:"password"`
songs []IncomingSong `json:"songs"`
}
type OutgoingRequest struct {
username string `json:"username"`
password string `json:"password"`
songs []OutgoingSong `json:"songs"`
}
type IncomingSong struct {
artist string `json:"artist"`
album string `json:"album"`
title string `json:"title"`
}
type OutgoingSong struct {
musician string `json:"musician"`
record string `json:"record"`
name string `json:"name"`
}
func main() {
http.HandleFunc("/songs/create", createSong)
http.ListenAndServe(":8080", nil)
}
func createSong(rw http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(req.Body)
var incomingRequest IncomingRequest
decoder.Decode(&incomingRequest)
outgoingRequest := incomingRequestToOutgoingRequest(incomingRequest)
r, _ := json.Marshal(outgoingRequest)
request, _ := http.NewRequest("POST", ThirdPartyApi, bytes.NewBuffer(r))
request.Header.Set("Content-Type", "application/json")
client := http.Client{}
response, _ := client.Do(request)
fmt.Fprintln(rw, response)
}
func incomingRequestToOutgoingRequest(inc IncomingRequest) OutgoingRequest {
outgoingRequest := OutgoingRequest{
username: inc.username,
password: inc.password,
}
for _, s := range inc.songs {
outgoingRequest.songs = append(
outgoingRequest.songs, OutgoingSong{
musician: s.artist,
record: s.album,
name: s.title,
},
)
}
return outgoingRequest
}
Vì vậy, tôi có thể nhấn ứng dụng chạy trên localhost:8080
với một cái gì đó như thế này:
curl -X POST http://localhost:8080/songs/new --data \
'{"username": "<my-username>", "password": "<my-password>", "songs": \
["artist": "<song-artist>", "title": "<song-title>", "album": "<song-album>"]}'
câu hỏi của tôi là: làm thế nào để tôi viết bài kiểm tra mà kiểm tra rằng yêu cầu đó đi ra ngoài (trong trường hợp này để http://www.coolsongssite.api
) là đúng, mà không hành động ual gửi nó?
Tôi có nên viết lại trình xử lý createSong
để tôi có thể cô lập những gì đang diễn ra trong client.Do(request)
không?
Bất kỳ trợ giúp/lời khuyên/điểm nào đúng hướng đều được đánh giá cao.
Ở đây tôi có thể kiểm tra incomingRequestToOutgoingRequest
như vậy:
package main
import (
"testing"
)
func TestincomingRequestToOutgoingRequest(t *testing.T) {
incomingRequest := IncomingRequest{
username: "myuser",
password: "mypassword",
}
var songs []IncomingSong
songs = append(
songs, IncomingSong{
artist: "White Rabbits",
album: "Milk Famous",
title: "I'm Not Me",
},
)
outgoingRequest := incomingRequestToOutgoingRequest(incomingRequest)
if outgoingRequest.songs[0].musician != "White Rabbits" {
t.Error("Expected musican name to be 'White Rabbits'. Got: ", outgoingRequest.songs[0].musician)
}
}
Cảm ơn bạn! Đây là những gì tôi đang tìm kiếm. –