MCPcopy
hub / github.com/mswjs/msw / text

Method text

src/core/HttpResponse.ts:65–96  ·  view source on GitHub ↗

* Create a `Response` with a `Content-Type: "text/plain"` body. * @example * HttpResponse.text('hello world') * HttpResponse.text('Error', { status: 500 })

(
    body?: NoInfer<BodyType> | null,
    init?: HttpResponseInit,
  )

Source from the content-addressed store, hash-verified

63 * HttpResponse.text('Error', { status: 500 })
64 */
65 static text<BodyType extends string>(
66 body?: NoInfer<BodyType> | null,
67 init?: HttpResponseInit,
68 ): HttpResponse<BodyType> {
69 const responseInit = normalizeResponseInit(init)
70 const hasExplicitContentType = responseInit.headers.has('Content-Type')
71
72 if (!hasExplicitContentType) {
73 responseInit.headers.set('Content-Type', 'text/plain')
74 }
75
76 // Automatically set the "Content-Length" response header
77 // for non-empty text responses. This enforces consistency and
78 // brings mocked responses closer to production.
79 if (!responseInit.headers.has('Content-Length')) {
80 responseInit.headers.set(
81 'Content-Length',
82 body ? new Blob([body]).size.toString() : '0',
83 )
84 }
85
86 const response = new HttpResponse(body, responseInit)
87
88 if (!hasExplicitContentType) {
89 Object.defineProperty(response, kDefaultContentType, {
90 value: true,
91 enumerable: false,
92 })
93 }
94
95 return response
96 }
97
98 /**
99 * Create a `Response` with a `Content-Type: "application/json"` body.

Calls 2

normalizeResponseInitFunction · 0.90
toStringMethod · 0.80

Tested by 3

stopWorkerOnFunction · 0.64
nextFunction · 0.64
runFunction · 0.64