musickit-api-mock-playwright 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- musickit_api_mock_playwright-0.0.1/LICENSE.txt +121 -0
- musickit_api_mock_playwright-0.0.1/PKG-INFO +123 -0
- musickit_api_mock_playwright-0.0.1/README.md +97 -0
- musickit_api_mock_playwright-0.0.1/pyproject.toml +54 -0
- musickit_api_mock_playwright-0.0.1/src/musickit_api_mock_playwright/__init__.py +6 -0
- musickit_api_mock_playwright-0.0.1/src/musickit_api_mock_playwright/async_adapter.py +49 -0
- musickit_api_mock_playwright-0.0.1/src/musickit_api_mock_playwright/py.typed +0 -0
- musickit_api_mock_playwright-0.0.1/src/musickit_api_mock_playwright/shared.py +27 -0
- musickit_api_mock_playwright-0.0.1/src/musickit_api_mock_playwright/sync_adapter.py +44 -0
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
Creative Commons Legal Code
|
|
2
|
+
|
|
3
|
+
CC0 1.0 Universal
|
|
4
|
+
|
|
5
|
+
CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
|
|
6
|
+
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
|
|
7
|
+
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
|
|
8
|
+
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
|
|
9
|
+
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
|
|
10
|
+
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
|
|
11
|
+
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
|
|
12
|
+
HEREUNDER.
|
|
13
|
+
|
|
14
|
+
Statement of Purpose
|
|
15
|
+
|
|
16
|
+
The laws of most jurisdictions throughout the world automatically confer
|
|
17
|
+
exclusive Copyright and Related Rights (defined below) upon the creator
|
|
18
|
+
and subsequent owner(s) (each and all, an "owner") of an original work of
|
|
19
|
+
authorship and/or a database (each, a "Work").
|
|
20
|
+
|
|
21
|
+
Certain owners wish to permanently relinquish those rights to a Work for
|
|
22
|
+
the purpose of contributing to a commons of creative, cultural and
|
|
23
|
+
scientific works ("Commons") that the public can reliably and without fear
|
|
24
|
+
of later claims of infringement build upon, modify, incorporate in other
|
|
25
|
+
works, reuse and redistribute as freely as possible in any form whatsoever
|
|
26
|
+
and for any purposes, including without limitation commercial purposes.
|
|
27
|
+
These owners may contribute to the Commons to promote the ideal of a free
|
|
28
|
+
culture and the further production of creative, cultural and scientific
|
|
29
|
+
works, or to gain reputation or greater distribution for their Work in
|
|
30
|
+
part through the use and efforts of others.
|
|
31
|
+
|
|
32
|
+
For these and/or other purposes and motivations, and without any
|
|
33
|
+
expectation of additional consideration or compensation, the person
|
|
34
|
+
associating CC0 with a Work (the "Affirmer"), to the extent that he or she
|
|
35
|
+
is an owner of Copyright and Related Rights in the Work, voluntarily
|
|
36
|
+
elects to apply CC0 to the Work and publicly distribute the Work under its
|
|
37
|
+
terms, with knowledge of his or her Copyright and Related Rights in the
|
|
38
|
+
Work and the meaning and intended legal effect of CC0 on those rights.
|
|
39
|
+
|
|
40
|
+
1. Copyright and Related Rights. A Work made available under CC0 may be
|
|
41
|
+
protected by copyright and related or neighboring rights ("Copyright and
|
|
42
|
+
Related Rights"). Copyright and Related Rights include, but are not
|
|
43
|
+
limited to, the following:
|
|
44
|
+
|
|
45
|
+
i. the right to reproduce, adapt, distribute, perform, display,
|
|
46
|
+
communicate, and translate a Work;
|
|
47
|
+
ii. moral rights retained by the original author(s) and/or performer(s);
|
|
48
|
+
iii. publicity and privacy rights pertaining to a person's image or
|
|
49
|
+
likeness depicted in a Work;
|
|
50
|
+
iv. rights protecting against unfair competition in regards to a Work,
|
|
51
|
+
subject to the limitations in paragraph 4(a), below;
|
|
52
|
+
v. rights protecting the extraction, dissemination, use and reuse of data
|
|
53
|
+
in a Work;
|
|
54
|
+
vi. database rights (such as those arising under Directive 96/9/EC of the
|
|
55
|
+
European Parliament and of the Council of 11 March 1996 on the legal
|
|
56
|
+
protection of databases, and under any national implementation
|
|
57
|
+
thereof, including any amended or successor version of such
|
|
58
|
+
directive); and
|
|
59
|
+
vii. other similar, equivalent or corresponding rights throughout the
|
|
60
|
+
world based on applicable law or treaty, and any national
|
|
61
|
+
implementations thereof.
|
|
62
|
+
|
|
63
|
+
2. Waiver. To the greatest extent permitted by, but not in contravention
|
|
64
|
+
of, applicable law, Affirmer hereby overtly, fully, permanently,
|
|
65
|
+
irrevocably and unconditionally waives, abandons, and surrenders all of
|
|
66
|
+
Affirmer's Copyright and Related Rights and associated claims and causes
|
|
67
|
+
of action, whether now known or unknown (including existing as well as
|
|
68
|
+
future claims and causes of action), in the Work (i) in all territories
|
|
69
|
+
worldwide, (ii) for the maximum duration provided by applicable law or
|
|
70
|
+
treaty (including future time extensions), (iii) in any current or future
|
|
71
|
+
medium and for any number of copies, and (iv) for any purpose whatsoever,
|
|
72
|
+
including without limitation commercial, advertising or promotional
|
|
73
|
+
purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
|
|
74
|
+
member of the public at large and to the detriment of Affirmer's heirs and
|
|
75
|
+
successors, fully intending that such Waiver shall not be subject to
|
|
76
|
+
revocation, rescission, cancellation, termination, or any other legal or
|
|
77
|
+
equitable action to disrupt the quiet enjoyment of the Work by the public
|
|
78
|
+
as contemplated by Affirmer's express Statement of Purpose.
|
|
79
|
+
|
|
80
|
+
3. Public License Fallback. Should any part of the Waiver for any reason
|
|
81
|
+
be judged legally invalid or ineffective under applicable law, then the
|
|
82
|
+
Waiver shall be preserved to the maximum extent permitted taking into
|
|
83
|
+
account Affirmer's express Statement of Purpose. In addition, to the
|
|
84
|
+
extent the Waiver is so judged Affirmer hereby grants to each affected
|
|
85
|
+
person a royalty-free, non transferable, non sublicensable, non exclusive,
|
|
86
|
+
irrevocable and unconditional license to exercise Affirmer's Copyright and
|
|
87
|
+
Related Rights in the Work (i) in all territories worldwide, (ii) for the
|
|
88
|
+
maximum duration provided by applicable law or treaty (including future
|
|
89
|
+
time extensions), (iii) in any current or future medium and for any number
|
|
90
|
+
of copies, and (iv) for any purpose whatsoever, including without
|
|
91
|
+
limitation commercial, advertising or promotional purposes (the
|
|
92
|
+
"License"). The License shall be deemed effective as of the date CC0 was
|
|
93
|
+
applied by Affirmer to the Work. Should any part of the License for any
|
|
94
|
+
reason be judged legally invalid or ineffective under applicable law, such
|
|
95
|
+
partial invalidity or ineffectiveness shall not invalidate the remainder
|
|
96
|
+
of the License, and in such case Affirmer hereby affirms that he or she
|
|
97
|
+
will not (i) exercise any of his or her remaining Copyright and Related
|
|
98
|
+
Rights in the Work or (ii) assert any associated claims and causes of
|
|
99
|
+
action with respect to the Work, in either case contrary to Affirmer's
|
|
100
|
+
express Statement of Purpose.
|
|
101
|
+
|
|
102
|
+
4. Limitations and Disclaimers.
|
|
103
|
+
|
|
104
|
+
a. No trademark or patent rights held by Affirmer are waived, abandoned,
|
|
105
|
+
surrendered, licensed or otherwise affected by this document.
|
|
106
|
+
b. Affirmer offers the Work as-is and makes no representations or
|
|
107
|
+
warranties of any kind concerning the Work, express, implied,
|
|
108
|
+
statutory or otherwise, including without limitation warranties of
|
|
109
|
+
title, merchantability, fitness for a particular purpose, non
|
|
110
|
+
infringement, or the absence of latent or other defects, accuracy, or
|
|
111
|
+
the present or absence of errors, whether or not discoverable, all to
|
|
112
|
+
the greatest extent permissible under applicable law.
|
|
113
|
+
c. Affirmer disclaims responsibility for clearing rights of other persons
|
|
114
|
+
that may apply to the Work or any use thereof, including without
|
|
115
|
+
limitation any person's Copyright and Related Rights in the Work.
|
|
116
|
+
Further, Affirmer disclaims responsibility for obtaining any necessary
|
|
117
|
+
consents, permissions or other rights required for any use of the
|
|
118
|
+
Work.
|
|
119
|
+
d. Affirmer understands and acknowledges that Creative Commons is not a
|
|
120
|
+
party to this document and has no duty or obligation with respect to
|
|
121
|
+
this CC0 or use of the Work.
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: musickit-api-mock-playwright
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Playwright host adapter for musickit-api-mock.
|
|
5
|
+
Keywords: apple-music,musickit,mock,testing,playwright,browser-automation
|
|
6
|
+
Author: HotariTobu
|
|
7
|
+
Author-email: HotariTobu <hotari24tools@gmail.com>
|
|
8
|
+
License-Expression: CC0-1.0
|
|
9
|
+
License-File: LICENSE.txt
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Topic :: Software Development :: Testing
|
|
17
|
+
Classifier: Topic :: Software Development :: Testing :: Mocking
|
|
18
|
+
Classifier: Typing :: Typed
|
|
19
|
+
Requires-Dist: musickit-api-mock
|
|
20
|
+
Requires-Dist: playwright>=1.58.0
|
|
21
|
+
Requires-Python: >=3.12
|
|
22
|
+
Project-URL: Homepage, https://github.com/HotariTobu/musickit-api-mock
|
|
23
|
+
Project-URL: Repository, https://github.com/HotariTobu/musickit-api-mock
|
|
24
|
+
Project-URL: Issues, https://github.com/HotariTobu/musickit-api-mock/issues
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# musickit-api-mock
|
|
28
|
+
|
|
29
|
+
Apple Music API mock for [MusicKit JS](https://developer.apple.com/musickit/). Runs as an in-process request interceptor inside browser-automation tests, so the page hits the mock instead of `api.music.apple.com`.
|
|
30
|
+
|
|
31
|
+
Useful for testing web apps that embed MusicKit JS without depending on Apple's servers, a developer token, or a signed-in Apple Music subscription.
|
|
32
|
+
|
|
33
|
+
## Packages
|
|
34
|
+
|
|
35
|
+
This repository is a uv workspace with a thin core / adapter split:
|
|
36
|
+
|
|
37
|
+
- **`musickit-api-mock`** — transport-agnostic mock engine. Routes HTTP requests, composes Apple Music API response bodies, and ships the in-page JS shim that replaces MusicKit JS's browser integrations.
|
|
38
|
+
- **`musickit-api-mock-playwright`** — Playwright host adapter. Bridges the engine to `page.route` for both sync and async Playwright APIs.
|
|
39
|
+
|
|
40
|
+
## Install
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pip install musickit-api-mock-playwright
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
The Playwright adapter depends on the core package transitively. Install only the core package if you write your own host adapter for a different browser-automation framework.
|
|
47
|
+
|
|
48
|
+
## Quick start
|
|
49
|
+
|
|
50
|
+
### Sync Playwright
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
from musickit_api_mock import (
|
|
54
|
+
MusicKitApiMock,
|
|
55
|
+
Song,
|
|
56
|
+
Storefront,
|
|
57
|
+
StorefrontResponseSuccess,
|
|
58
|
+
)
|
|
59
|
+
from musickit_api_mock_playwright import intercept
|
|
60
|
+
from playwright.sync_api import sync_playwright
|
|
61
|
+
|
|
62
|
+
mock = MusicKitApiMock()
|
|
63
|
+
|
|
64
|
+
# Shared resource data: songs keyed by catalog id.
|
|
65
|
+
mock.data.songs = {
|
|
66
|
+
"1000000001": Song.from_file("tests/fixtures/silence.m4a"),
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
# Endpoint-level response: storefront resolution.
|
|
70
|
+
mock.endpoints.storefront = StorefrontResponseSuccess(
|
|
71
|
+
storefront=Storefront(
|
|
72
|
+
id="us",
|
|
73
|
+
name="United States",
|
|
74
|
+
default_language_tag="en-US",
|
|
75
|
+
supported_language_tags=["en-US"],
|
|
76
|
+
explicit_content_policy="allowed",
|
|
77
|
+
),
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
with sync_playwright() as pw:
|
|
81
|
+
browser = pw.chromium.launch()
|
|
82
|
+
page = browser.new_page()
|
|
83
|
+
intercept(mock, page)
|
|
84
|
+
page.goto("https://your-app.example/")
|
|
85
|
+
# ... drive MusicKit JS through your app and assert ...
|
|
86
|
+
browser.close()
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Async Playwright
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
from musickit_api_mock_playwright import intercept_async
|
|
93
|
+
|
|
94
|
+
await intercept_async(mock, page)
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
`intercept` and `intercept_async` accept either a `Page` (binds to that single page) or a `BrowserContext` (covers every page in the context, including popups and pages opened later).
|
|
98
|
+
|
|
99
|
+
## Surfaces
|
|
100
|
+
|
|
101
|
+
A `MusicKitApiMock` instance exposes three configuration surfaces. The split is load-bearing — each surface has a distinct semantic role:
|
|
102
|
+
|
|
103
|
+
- **`mock.data.*`** — shared resource sources (songs, albums, playlists, artists, library items, ...). Read by multiple endpoints when composing responses. Each field accepts a `dict[str, T]` keyed by id, or a `Callable[[LookupContext], T]` for dynamic resolution.
|
|
104
|
+
- **`mock.endpoints.*`** — per-endpoint response overrides (storefront, account, license, web playback, ...). Each field accepts a response value, a dict keyed by id, or a callable returning a response. Use these to shape the HTTP response itself (status, error variants, ...).
|
|
105
|
+
- **`mock.browser.*`** — state consumed by the in-page JS shim, e.g. the authorize response delivered when the page calls `music.authorize()`, and the EME key system flavor the shim should expose.
|
|
106
|
+
|
|
107
|
+
All fields default to `None`, which is the unset sentinel. Reading an unset field at request time raises `ValueError` — the mock does not invent fallback values for fields you didn't configure.
|
|
108
|
+
|
|
109
|
+
## Scope
|
|
110
|
+
|
|
111
|
+
- **Target audience**: third-party developers using the default MusicKit JS configuration, i.e. `MusicKit.configure(...)` without Apple-internal overrides.
|
|
112
|
+
- **Intercepted host**: `api.music.apple.com`. Apple's own web-player override hosts (e.g. `amp-api.music.apple.com`) are out of scope.
|
|
113
|
+
- **Intercepted paths**: every path the default-config host emits within MusicKit JS's resource set (songs, albums, artists, library-*, me/*, ...). Within an intercepted path, every form the path can accept is handled — not just the subset MusicKit JS happens to send.
|
|
114
|
+
|
|
115
|
+
## Supported environments
|
|
116
|
+
|
|
117
|
+
- Python ≥ 3.12
|
|
118
|
+
- Playwright (sync and async) on Chromium, Firefox, and WebKit
|
|
119
|
+
- Linux, macOS, Windows
|
|
120
|
+
|
|
121
|
+
## License
|
|
122
|
+
|
|
123
|
+
[CC0 1.0 Universal](LICENSE.txt) — public domain dedication.
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# musickit-api-mock
|
|
2
|
+
|
|
3
|
+
Apple Music API mock for [MusicKit JS](https://developer.apple.com/musickit/). Runs as an in-process request interceptor inside browser-automation tests, so the page hits the mock instead of `api.music.apple.com`.
|
|
4
|
+
|
|
5
|
+
Useful for testing web apps that embed MusicKit JS without depending on Apple's servers, a developer token, or a signed-in Apple Music subscription.
|
|
6
|
+
|
|
7
|
+
## Packages
|
|
8
|
+
|
|
9
|
+
This repository is a uv workspace with a thin core / adapter split:
|
|
10
|
+
|
|
11
|
+
- **`musickit-api-mock`** — transport-agnostic mock engine. Routes HTTP requests, composes Apple Music API response bodies, and ships the in-page JS shim that replaces MusicKit JS's browser integrations.
|
|
12
|
+
- **`musickit-api-mock-playwright`** — Playwright host adapter. Bridges the engine to `page.route` for both sync and async Playwright APIs.
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pip install musickit-api-mock-playwright
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
The Playwright adapter depends on the core package transitively. Install only the core package if you write your own host adapter for a different browser-automation framework.
|
|
21
|
+
|
|
22
|
+
## Quick start
|
|
23
|
+
|
|
24
|
+
### Sync Playwright
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from musickit_api_mock import (
|
|
28
|
+
MusicKitApiMock,
|
|
29
|
+
Song,
|
|
30
|
+
Storefront,
|
|
31
|
+
StorefrontResponseSuccess,
|
|
32
|
+
)
|
|
33
|
+
from musickit_api_mock_playwright import intercept
|
|
34
|
+
from playwright.sync_api import sync_playwright
|
|
35
|
+
|
|
36
|
+
mock = MusicKitApiMock()
|
|
37
|
+
|
|
38
|
+
# Shared resource data: songs keyed by catalog id.
|
|
39
|
+
mock.data.songs = {
|
|
40
|
+
"1000000001": Song.from_file("tests/fixtures/silence.m4a"),
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
# Endpoint-level response: storefront resolution.
|
|
44
|
+
mock.endpoints.storefront = StorefrontResponseSuccess(
|
|
45
|
+
storefront=Storefront(
|
|
46
|
+
id="us",
|
|
47
|
+
name="United States",
|
|
48
|
+
default_language_tag="en-US",
|
|
49
|
+
supported_language_tags=["en-US"],
|
|
50
|
+
explicit_content_policy="allowed",
|
|
51
|
+
),
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
with sync_playwright() as pw:
|
|
55
|
+
browser = pw.chromium.launch()
|
|
56
|
+
page = browser.new_page()
|
|
57
|
+
intercept(mock, page)
|
|
58
|
+
page.goto("https://your-app.example/")
|
|
59
|
+
# ... drive MusicKit JS through your app and assert ...
|
|
60
|
+
browser.close()
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Async Playwright
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
from musickit_api_mock_playwright import intercept_async
|
|
67
|
+
|
|
68
|
+
await intercept_async(mock, page)
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
`intercept` and `intercept_async` accept either a `Page` (binds to that single page) or a `BrowserContext` (covers every page in the context, including popups and pages opened later).
|
|
72
|
+
|
|
73
|
+
## Surfaces
|
|
74
|
+
|
|
75
|
+
A `MusicKitApiMock` instance exposes three configuration surfaces. The split is load-bearing — each surface has a distinct semantic role:
|
|
76
|
+
|
|
77
|
+
- **`mock.data.*`** — shared resource sources (songs, albums, playlists, artists, library items, ...). Read by multiple endpoints when composing responses. Each field accepts a `dict[str, T]` keyed by id, or a `Callable[[LookupContext], T]` for dynamic resolution.
|
|
78
|
+
- **`mock.endpoints.*`** — per-endpoint response overrides (storefront, account, license, web playback, ...). Each field accepts a response value, a dict keyed by id, or a callable returning a response. Use these to shape the HTTP response itself (status, error variants, ...).
|
|
79
|
+
- **`mock.browser.*`** — state consumed by the in-page JS shim, e.g. the authorize response delivered when the page calls `music.authorize()`, and the EME key system flavor the shim should expose.
|
|
80
|
+
|
|
81
|
+
All fields default to `None`, which is the unset sentinel. Reading an unset field at request time raises `ValueError` — the mock does not invent fallback values for fields you didn't configure.
|
|
82
|
+
|
|
83
|
+
## Scope
|
|
84
|
+
|
|
85
|
+
- **Target audience**: third-party developers using the default MusicKit JS configuration, i.e. `MusicKit.configure(...)` without Apple-internal overrides.
|
|
86
|
+
- **Intercepted host**: `api.music.apple.com`. Apple's own web-player override hosts (e.g. `amp-api.music.apple.com`) are out of scope.
|
|
87
|
+
- **Intercepted paths**: every path the default-config host emits within MusicKit JS's resource set (songs, albums, artists, library-*, me/*, ...). Within an intercepted path, every form the path can accept is handled — not just the subset MusicKit JS happens to send.
|
|
88
|
+
|
|
89
|
+
## Supported environments
|
|
90
|
+
|
|
91
|
+
- Python ≥ 3.12
|
|
92
|
+
- Playwright (sync and async) on Chromium, Firefox, and WebKit
|
|
93
|
+
- Linux, macOS, Windows
|
|
94
|
+
|
|
95
|
+
## License
|
|
96
|
+
|
|
97
|
+
[CC0 1.0 Universal](LICENSE.txt) — public domain dedication.
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "musickit-api-mock-playwright"
|
|
3
|
+
version = "0.0.1"
|
|
4
|
+
description = "Playwright host adapter for musickit-api-mock."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "HotariTobu", email = "hotari24tools@gmail.com" }
|
|
8
|
+
]
|
|
9
|
+
license = "CC0-1.0"
|
|
10
|
+
license-files = ["LICENSE.txt"]
|
|
11
|
+
requires-python = ">=3.12"
|
|
12
|
+
keywords = ["apple-music", "musickit", "mock", "testing", "playwright", "browser-automation"]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 3 - Alpha",
|
|
15
|
+
"Intended Audience :: Developers",
|
|
16
|
+
"Operating System :: OS Independent",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Programming Language :: Python :: 3.12",
|
|
19
|
+
"Programming Language :: Python :: 3.13",
|
|
20
|
+
"Topic :: Software Development :: Testing",
|
|
21
|
+
"Topic :: Software Development :: Testing :: Mocking",
|
|
22
|
+
"Typing :: Typed",
|
|
23
|
+
]
|
|
24
|
+
dependencies = [
|
|
25
|
+
"musickit-api-mock",
|
|
26
|
+
"playwright>=1.58.0",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[project.urls]
|
|
30
|
+
Homepage = "https://github.com/HotariTobu/musickit-api-mock"
|
|
31
|
+
Repository = "https://github.com/HotariTobu/musickit-api-mock"
|
|
32
|
+
Issues = "https://github.com/HotariTobu/musickit-api-mock/issues"
|
|
33
|
+
|
|
34
|
+
[build-system]
|
|
35
|
+
requires = ["uv_build>=0.11.2,<0.12.0"]
|
|
36
|
+
build-backend = "uv_build"
|
|
37
|
+
|
|
38
|
+
[tool.uv.sources]
|
|
39
|
+
musickit-api-mock = { workspace = true }
|
|
40
|
+
|
|
41
|
+
[dependency-groups]
|
|
42
|
+
dev = [
|
|
43
|
+
"pytest-asyncio>=1.3.0",
|
|
44
|
+
"pytest-playwright-asyncio>=0.7.2",
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
[tool.pytest.ini_options]
|
|
48
|
+
asyncio_mode = "auto"
|
|
49
|
+
asyncio_default_fixture_loop_scope = "session"
|
|
50
|
+
asyncio_default_test_loop_scope = "session"
|
|
51
|
+
markers = [
|
|
52
|
+
"sync_test: tests using the sync_playwright API",
|
|
53
|
+
"async_test: tests using the async_playwright API",
|
|
54
|
+
]
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""Async Playwright adapter."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
from typing import TYPE_CHECKING
|
|
7
|
+
|
|
8
|
+
from musickit_api_mock_playwright.shared import _to_core_request
|
|
9
|
+
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from musickit_api_mock.mock import MusicKitApiMock
|
|
12
|
+
from playwright.async_api import BrowserContext as AsyncBrowserContext
|
|
13
|
+
from playwright.async_api import Page as AsyncPage
|
|
14
|
+
from playwright.async_api import Route as AsyncRoute
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
async def intercept_async(
|
|
18
|
+
mock: MusicKitApiMock,
|
|
19
|
+
target: AsyncPage | AsyncBrowserContext,
|
|
20
|
+
) -> None:
|
|
21
|
+
"""Wire the mock into an async Playwright page or browser context.
|
|
22
|
+
|
|
23
|
+
Pass a page to bind to that single page. Pass a browser context to cover
|
|
24
|
+
every page in the context, including popups and pages opened later.
|
|
25
|
+
"""
|
|
26
|
+
await target.add_init_script(mock.get_shim_script())
|
|
27
|
+
await target.route("**/*", lambda route: _handle_async(mock, route))
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
async def _handle_async(mock: MusicKitApiMock, route: AsyncRoute) -> None:
|
|
31
|
+
req = _to_core_request(route.request)
|
|
32
|
+
try:
|
|
33
|
+
resp = mock.handle_request(req)
|
|
34
|
+
except Exception as e:
|
|
35
|
+
# The mock raised (e.g. a setter is unset). Surface the original
|
|
36
|
+
# message to stderr (it would otherwise be lost in the network-level
|
|
37
|
+
# abort) and end the request as a network failure rather than hanging.
|
|
38
|
+
# Returning any HTTP status here would collide with mock-produced
|
|
39
|
+
# status responses, so abort is the only collision-free choice.
|
|
40
|
+
print(
|
|
41
|
+
f"[musickit-api-mock] handler error: {type(e).__name__}: {e}",
|
|
42
|
+
file=sys.stderr,
|
|
43
|
+
)
|
|
44
|
+
await route.abort()
|
|
45
|
+
return
|
|
46
|
+
if resp is None:
|
|
47
|
+
await route.fallback()
|
|
48
|
+
return
|
|
49
|
+
await route.fulfill(status=resp.status, headers=resp.headers, body=resp.body)
|
|
File without changes
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""Shared helper to convert a Playwright ``Request`` into the core ``Request`` type."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import TYPE_CHECKING
|
|
6
|
+
|
|
7
|
+
from musickit_api_mock.transport.http import Request
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from playwright.async_api import Request as AsyncRequest
|
|
11
|
+
from playwright.sync_api import Request as SyncRequest
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _to_core_request(playwright_request: SyncRequest | AsyncRequest) -> Request:
|
|
15
|
+
body: bytes | None = None
|
|
16
|
+
raw = playwright_request.post_data_buffer
|
|
17
|
+
if raw is not None:
|
|
18
|
+
body = bytes(raw)
|
|
19
|
+
elif playwright_request.post_data is not None:
|
|
20
|
+
body = playwright_request.post_data.encode("utf-8")
|
|
21
|
+
headers_dict: dict[str, str] = dict(playwright_request.headers or {})
|
|
22
|
+
return Request(
|
|
23
|
+
method=playwright_request.method,
|
|
24
|
+
url=playwright_request.url,
|
|
25
|
+
headers=headers_dict,
|
|
26
|
+
body=body,
|
|
27
|
+
)
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""Sync Playwright adapter."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
from typing import TYPE_CHECKING
|
|
7
|
+
|
|
8
|
+
from musickit_api_mock_playwright.shared import _to_core_request
|
|
9
|
+
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from musickit_api_mock.mock import MusicKitApiMock
|
|
12
|
+
from playwright.sync_api import BrowserContext, Page, Route
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def intercept(mock: MusicKitApiMock, target: Page | BrowserContext) -> None:
|
|
16
|
+
"""Wire the mock into a sync Playwright page or browser context.
|
|
17
|
+
|
|
18
|
+
Pass a page to bind to that single page. Pass a browser context to cover
|
|
19
|
+
every page in the context, including popups and pages opened later.
|
|
20
|
+
"""
|
|
21
|
+
target.add_init_script(mock.get_shim_script())
|
|
22
|
+
target.route("**/*", lambda route: _handle(mock, route))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _handle(mock: MusicKitApiMock, route: Route) -> None:
|
|
26
|
+
req = _to_core_request(route.request)
|
|
27
|
+
try:
|
|
28
|
+
resp = mock.handle_request(req)
|
|
29
|
+
except Exception as e:
|
|
30
|
+
# The mock raised (e.g. a setter is unset). Surface the original
|
|
31
|
+
# message to stderr (it would otherwise be lost in the network-level
|
|
32
|
+
# abort) and end the request as a network failure rather than hanging.
|
|
33
|
+
# Returning any HTTP status here would collide with mock-produced
|
|
34
|
+
# status responses, so abort is the only collision-free choice.
|
|
35
|
+
print(
|
|
36
|
+
f"[musickit-api-mock] handler error: {type(e).__name__}: {e}",
|
|
37
|
+
file=sys.stderr,
|
|
38
|
+
)
|
|
39
|
+
route.abort()
|
|
40
|
+
return
|
|
41
|
+
if resp is None:
|
|
42
|
+
route.fallback()
|
|
43
|
+
return
|
|
44
|
+
route.fulfill(status=resp.status, headers=resp.headers, body=resp.body)
|