小编Dre*_*zek的帖子

测试Go(Golang)API传出请求而不实际触及第三方API

我正在开发一个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)

testing api go

6
推荐指数
1
解决办法
942
查看次数

标签 统计

api ×1

go ×1

testing ×1