我正在开发一个Go API,它在内部后端和几个第三方API之间进行转换.我试图了解如何在不实际使用外部API的情况下测试其功能.
例如,这是一个处理传入请求以制作新歌曲的服务器,并将请求发送给第三方API:
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)
} …Run Code Online (Sandbox Code Playgroud)